0

How can I send and receive values?

What is it that I'm doing wrong?

jQuery: on click of save button

$("#budget_submit").click(function(){       
        var budget = $("#budget_cardno").val();
        $.ajax({
            type: 'POST',
            dataType: "json",
            url: 'budget.php',
            data: budget,
            success: function(data){
                if(data.success == true)
                alert(data.message);
        }
        });
    });

html:

<form method="post" id="budgetform">
   <fieldset>
      <ul>
    <li>
      <label>CARD NUMBER</label>
      <input name="budget_cardno" id="budget_cardno" />
    </li>
    <li>
      <label>MONTHLY BUDGET</label>
      <input name="budget_monthly" id="budget_monthly" />
    </li>
    <li>
      <input class="submit" id="budget_submit" name="budget_submit" type="submit" value="Save"/>
        </li>
      </ul>
   </fieldset>
</form>

Php:

<?php 
$dbhost = '#';
$dbuser = '#';
$dbpass = '#';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('Could not connect: ' . mysql_error());
}

$cardNo = $_POST['budget'];     #This statement does not work.
if($cardNo == "")
{
    mysql_close($conn);
    $data = array('success' => true, 'message' => 'ENTER DETAILS');
    echo json_encode($data);
}

else
{
}
?>

how can I send the value budget to the PHP file and store it in $cardNo and return the message?

I don't want to check for the condition at the client side because I have other conditions to check which needs data from database.

1
  • you can check the $_POST received by the php script by using echo print_r($_POST); it will tell you the key and the value of the data received (if any). I don'T see any trouble at first glance. You might want to consider using cosole tools like firebug or something similar to see what is sent and retuned. Commented Nov 18, 2012 at 2:59

2 Answers 2

1

actually this seems weird to me.

if($cardNo == "")
{
    mysql_close($conn);
    $data = array('success' => true, 'message' => 'ENTER DETAILS');
    echo json_encode($data);
}

This will trigger only if the value is EMPTY. So if you send a value, it won't trigger since the statement is false. Change your == to != so it state: if $cardNo IS NOT EMPTY like so:

if($cardNo != "")
{
    mysql_close($conn);
    $data = array('success' => true, 'message' => 'ENTER DETAILS');
    echo json_encode($data);
}
Sign up to request clarification or add additional context in comments.

2 Comments

further more, i sugest you start using mysqli instead of mysql as it is (or soon will be) deprecated.
I'm just checking whether the code works for empty value. The actual condition is different from the one I posted.
1

I think the problem is that you're treating the incoming data as a normal form, when in fact you're sending JSON. PHP is therefore not finding any $_POST parameter with name 'budget'.

You need to get the raw post data using file_get_contents('php://input'); and then use json_decode() to convert it into an object. From it you could then get the budget parameter.

So something like this should work:

$formdata = file_get_contents('php://input');
$formdata_obj = json_decode($formdata);

$cardNo = $formdata_obj->budget;

If you're actually just sending one value (as you seem to be doing) the first line alone might suffice, because the whole data would be the value itself rather than a JSON encoded object:

$cardNo = file_get_contents('php://input');

8 Comments

Just saw the same thing over here.
@jbx: how to use file_get_contents('php://input');
@jbx: $requestBody = file_get_contents('php://input'); $requestBody = json_decode($requestBody); $cardNo = $requestBody -> budget;
Yeah, but it has to be json_decode() not jsonDecode(), and don't use the same variable for 2 different things ($requestBody) it just confuses you. Why don't you copy my example?
If it still doesn't work put some 'echo' lines here and there to see what you are receiving in the file_get_contents (to make sure that you are really receiving the JSON), and maybe use var_export() to view the contents of the object generated from json_decode(). We've put you on the right track, we can't debug your application!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.