This might seem like a duplicate question, but it isn't. I have searched google but my bug remains.
The issue is that the php script can not set $_POST array to the value being passed by the ajax post request. I have a form that,when submitted,displays another form in the same divs as the first form. The second form contains a submit button, as well as a button which sends ajax post request but it's value is not being set by php. The code is as follows:
html code: it is to pick out value of the span.
<i>Current Balance: <span id="bal_val"><?php echo $b; ?></span></i>
<div id="bal" style='color:green;'>
Ajax code: here, edit button is a button (of the first form) which enables the display of the second form. The second form contains the balbutton input,which sends ajax request.
$('#edit').click(function(event){
event.preventDefault();
$('#bal').html('<button class="btn1 btn-1 btn-1b" id="balbutton" name="balbutton">Add 1000 Rs.</button>');
});
$('#balbutton').click(function(event){
event.preventDefault();
var bal = document.getElementById('bal_val').innerHTML;
var balance = parseInt(bal);
var dataString = {balance};
$.ajax({
type: "POST",
url: "admin.php/",
data: dataString,
contentType: 'application/x-www-form-urlencoded',
cache: false,
dataType: 'text',
success: function(result){
var data="";
console.log(result);
$('#bal').html('We will add 1000rs to your account soon');
},
error: function(error){console.log(error);}
});
});
php code:
<?php
$bal=$_POST['balance'];
$bal=intval($bal);
echo $bal;
echo '<pre>';
echo print_r($_POST);
echo '</pre><br>';
echo '<pre>';
echo var_dump($_POST);
echo '</pre>';
?>
Also, I have tried the solutions of:
Snapshot of the ajax post request and PHP error