simran dhamija simran dhamija - 7 months ago 27
Ajax Question

Undefined index on ajax post request

enter image description hereThis 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.

$('#bal').html('<button class="btn1 btn-1 btn-1b" id="balbutton" name="balbutton">Add 1000 Rs.</button>');


var bal = document.getElementById('bal_val').innerHTML;
var balance = parseInt(bal);
var dataString = {balance};

type: "POST",
url: "admin.php/",
data: dataString,
contentType: 'application/x-www-form-urlencoded',
cache: false,
dataType: 'text',
success: function(result){
var data="";
$('#bal').html('We will add 1000rs to your account soon');
error: function(error){console.log(error);}


php code:



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:

  1. Undefined index: Error in ajax POST and/or php script?

    1. jquery ajax => Undefined index php

    2. Undefined index in PHP post with AJAX

    3. Undefined index error in php using ajax


Snapshot of the ajax post request and PHP error

Jquery ajax post on console and php error


In this part, you are not giving a valid syntax:

var dataString = {balance};

Change the above to:

var dataString = {balance: balance};