Reputation: 1169
I have no idea why I am getting this weird error!
PHP Notice: Undefined index: refId in /var/www/echo.php on line 5
I am getting Console output, but cant echo refId
. Have I done anything wrong here?
<?php
$rollUrl = 34;
$refId = $_POST['refId'];
echo $refId;
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$.ajax({
url:'echo.php',
type: 'POST',
data: { 'refId': "<?php echo $rollUrl ?>" },
success: function(response){
console.log('Getting response');
}
});
</script>
Upvotes: 2
Views: 1119
Reputation:
Please see the comments in the code below:
<?php
$rollUrl = 34;
//Only try to process POST if there is something posted *and* refId exists
if (count($_POST) > 0 && isset($_POST['refId'])) {
$refId = $_POST['refId'];
echo $refId;
//Exit after echoing out the refId so that the HTML below does not also get returned.
exit();
}
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$.ajax({
url:'echo.php',
type: 'POST',
data: { 'refId': "<?php echo $rollUrl ?>" },
success: function(response) {
//Updated log to show the actual response received.
console.log('Getting response of "' + response + '"');
}
});
</script>
That works for me when I tested without any errors being thrown and the Ajax being executed.
Upvotes: 1
Reputation: 3622
This is happening because your variable is not set. Use isset
<?php
$rollUrl=34;
if(isset($_POST['refId'])) {
$refId=$_POST['refId'];
echo $refId;
}
?>
Update:
You should assign the refId
as a name attribute to any input field to revive the input from user.
<input type="text" name="refId" />
Upvotes: 0