dongzangchui2072 2012-11-18 02:55
浏览 30

如何使用JQuery-AJAX,JSON和php发送和接收对象或值?

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.

  • 写回答

2条回答 默认 最新

  • duanci9305 2012-11-18 03:03
    关注

    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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据