javascript - how to pass a jquery variable to php and then display it
65
Problem in passing Jquery variable to php .
When i pass variable id toB.php
i am getting this error
"Notice: Undefined index: id1 in C:\xampp \htdocs\PhpProject1\OtherUsableItems\B.php on line 2
How to solve this problem ???? ThisA.php
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
var id = 23;
$("#submit").click(function(){
$.post("B.php",
{
id1: id,
} );
});
});
</script>
</head>
<body>
<form action="B.php" method="post" >
<input type="submit" id="submit" name="submit"/>
</form>
</body>
</html>
ThisB.php
<?php
$a =$_POST['id1'];
echo $a ;
?>
I want id variable to pass on toB.php
Answer
Solution:
Try this one:
Then B.php
Answer
Solution:
Try changing the input type to button instead of submit
<input type="button" id="submit" name="submit"/>
Answer
Solution:
The problem is that your form is submited ,
$.post
is for ajax sumit. For that you need to prevent form submit.Add
event.preventDefault();
in your button click functionAnswer
Solution:
Use a hidden field to set the value you want to send across. Since you are using submit, your entire form is submitted. The hidden field is also sent and this can be accessed directly using the name of the hidden field.
Answer
Solution:
An E_NOTICE is thrown by php because the index of the POST-Array doesnt exist. You can switch error_reporting of for notices, see http://www.php.net/manual/de/errorfunc.configuration.php#ini.error-reporting or check if the index exists:
Answer
Solution:
Try code above. First: JS does not allow to do something like this:
{id1: id,}
(see comma at the end). That is a feature of PHP. Actually, your JS code should not be executed at all because that is a syntax error (suppose you will find it in console of your browser)Another problem: you must prevent default submit of a form.
e.preventDefault()
is needed for that. Without it, ajax post will be started and than usual form submit will happen. And your form has no element with name id1, suppose that second request produce that error message.EDIT: This is to post a value with regular submit, so browser is redirected to B.php
Answer
Solution:
Try this code,
Here you can pass value of 'id' to B.php,And will be access as $_POST['id1']
Answer
Solution:
Try this:
Since you are using ajax feature so you have to stop the form submit feature. You have a trailing comma here
{id1 : id,}
this is an issue in IE browsers but latest other browsers will just work fine.Note:
You posted this error:
"Notice: Undefined index: id1 in C:\xampp \htdocs......
Don't run the page in filesystem
$.post()
won't work this way.Instead try using
http://localhost/yourapplication/page.php
Answer
Solution:
Or you could use submit() this way
And form
EDIT: Oh and yeah for B.php isset check or error_reporting(0);