hgnisitida hgnisitida - 4 months ago 16
MySQL Question

PHP GET ($_GET) always prints name of variable and not value

what I'm trying to do is get the value of the selected term from the autocomplete menu (ui.item.label), store it in a variable and send that value to my database. For that, I have another php file named "proceed.php", which connects us to the database and will insert the data into the table.

But I seem to be stuck at passing the variable "$selected" from look to proceed as no matter what I do, the browser displays $vari, that is, the variable passed here -

proceed1.php?var=$vari


Backstory - You may notice that I have assigned the variable vari as apple. Yet, I see only $vari as the output on the browser and not apple.

Any help will be deeply appreciated.

Thank you very much.

look.php

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(function() {
$( "#route" ).autocomplete({
source: 'search.php',
select: function( event , ui ) {
$selected=ui.item.label;}
});
});
$vari="apple";
</script>

</head>
<link rel="stylesheet" type="text/css" href="look.css" media="screen" />
<body>
<div class="ui-widget">
<label for="route">Router Name: </label>
<input id="route">
<br><br><br>
<a href='proceed1.php?var=$vari' id=btn2 class="btn">Proceed</a>
</div>
</body>
</html>


proceed.php

<?php
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'searchrouters';
//connect with the database
$db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
//get search term
$vari = $_GET['var'];
echo $vari;
?>


UPDATE :

Thank you, the solution provided in the comments worked. But now when I'm trying to get the variable selected's value as :

<?php
$vari=$_GET['selected'];
?>


and then pass it in to the next page as :

<a href='proceed1.php?var=<?php echo $vari;?>' id=btn2 class="btn">Proceed</a>


I get the following error on the browser :


Undefined index: selected in C:\wamp\www\look.php on line 19

Answer

You need to modify you code in two places

<script>
  $(function() {
    $( "#route" ).autocomplete({
      source: 'search.php',
      select: function( event , ui ) {
         $selected=ui.item.label;
         $('#btn2').attr({'href':'proceed1.php?var='+$selected});
       } 
    }); 
  });
  </script>
<?php
  $vari="apple";
?>

and <a href="proceed1.php?term=<?php echo $vari;?>" id=btn2 class="btn">Proceed</a>

and when you need to access the variable just use $_GET['var'] Where ever you want to user php with HTML you need to user the <?php php code here ?>

here is a demo