Reputation: 259
I have the below button in my index.php file.
<form action="update.php" method="post" >
<button onclick="myFunction()">Update</button>
</form>
I have the below javascript function.
<script type="text/javascript">
function myFunction()
{
$.ajax({
type: 'post',
url: 'update.php',
data: {
source1: "some text",
},
success: function( data ) {
console.log( data );
}
});
}
</script>
I am using the above javascript function to send the javascript variable to the php server side script file (update.php). I am accessing the variable in update.php as below.
$src1= $_POST['source1'];
echo $src1;
However, I am not able to see the variable in the output of update.php file. What am doing wrong?
Upvotes: 0
Views: 65
Reputation: 8969
Have you tried .done
method instead of success
parameter which is supposed to be deprecated.
$.ajax({
type: 'post',
url: 'update.php',
data: {
source1: "some text",
},
}).done(function( data ) {
console.log( data );
});
Upvotes: 1
Reputation: 94101
Try to do it on the submit
event of the form and cancel the default behavior. First set the button type to submit the form:
<form action="update.php" method="post" >
<button type="submit">Update</button>
</form>
Then add the event in JavaScript:
$('form').submit(function(e) {
e.preventDefault();
// code here
});
Upvotes: 1