I can't find the issue with my code. On the second page (page2.php) the variable $productid is not there (is null). UPDATE: I forgot to mention I have session_start(); at the beginning of both page1.php and page2.php.
On the first page ex: page1.php I have this:
$(function() {
$( "#product" ).autocomplete({
source: 'searchproduct.php',
select: function (event, ui) {
var lbl = ui.item.label;
var value = ui.item.value;
//store in session
$.ajax({
type: "POST",
url: 'page1.php',
data: { value : value },
success: function(data)
{
$('#productform input[name="jProductID"]').val(value);
$("#productform").submit();
}
});
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<?php
$productid = $_POST['jProductID'];
$_SESSION['Sproductid'] = $productid;
?>
<form method="post" action="page2.php" id="productform">
<input type="hidden" name="jProductID" value="productID" />
<label for="product">Produs: </label>
<input id="product" name="productid" id="productid">
<input type="submit" value="Adauga">
</form>
On the second page ex: page2.php I have this:
$productid = $_SESSION['Sproductid'];
echo $productid;
Note: I'm very new to both php and javascript so any advice will be more than appreciated.
</div>
Solved, the code is correct, I was having an error in other part of my code, all the code I posted was inside an if(isset($_GET['...'])) {} wich was returning false. Thank you for your answers.
Just to be sure I replaced
<?php
$productid = $_POST['jProductID'];
$_SESSION['Sproductid'] = $productid;
?>
With
<?php
$productid = $_POST['value'];
$_SESSION['Sproductid'] = $productid;
?>
And I removed the
<input type="hidden" name="jProductID" value="productID" />
And the
$('#productform input[name="jProductID"]').val(value);