成功功能无警报

时间:2013-12-06 10:14:58

标签: php ajax jquery

我正在尝试从jquery ajax在数据库中插入值,并且我希望无论何时数据插入成功,结果输出都会成为其他明智的“错误:失败”。我在数据库中的条目已成功更新,但当我发出警报(msg)时,它不会给我消息。

          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">    </script>
  <body>
    <div class="wrapper">
    <div id="main" style="padding:50px 0 0 0;">
         <!-- Form -->
       <form id="contact-form"  method="post">
        <h3>Paypal Payment Details</h3>
        <div class="controls">
            <label>
                <span>TagId</span>
                <input placeholder="Please enter TagId" id="tagid" type="text" tabindex="1" >
            </label>
        </div>
        <div class="controls">
            <label>
                <span>Paypal Email: (required)</span>
                <input placeholder="All Payment will be collected in this email address" id="email" type="email" tabindex="2">
            </label>
        </div>
        <div class="controls">
            <label>
                <span>Amount</span>
                <input placeholder="Amount you would like to charged in GBP" id="amount" type="tel" tabindex="3">
            </label>
        </div>
                    <div class="controls">
                 <div id="error_div"></div>
                 </div>
        <div>
<button name="submit" type="submit" id="form-submit">Submit Detail</button>
        </div>
    </form>
    <!-- /Form -->

    </div>
</div>
 <script type="text/javascript">
        $(document).ready(function(){
   $('#form-submit').click(function()
    {
    var tagid = $("#tagid").val();
var email = $("#email").val();
var amount = $("#amount").val();
var param = 'tagid='+ tagid + '&email=' + email + '&amount=' + amount;
    param = param + '&type=assign_amount';
    locurl = 'dbentry.php';
          $.ajax({
              url: locurl,
              type:'post',
              data:param,
              success:function(msg)
                {  
                    alert(msg);            
                }
              });
});
  });

dbentry.php

  <?php
$vals = $_POST;
    include 'dbconfig.php';

    if($vals['type'] == "assign_amount")
{
    $values = assign_amount();  
    echo json_encode(array('status' =>$values));    
}


function assign_amount()
{
    global $con;
    global $vals;
    $sql = "INSERT INTO `dynamic_url`(`tagid`,`email`,`amount`) VALUES('".$vals['tagid']."','".$vals['email']."','".$vals['amount']."')";
    $result = mysql_query($sql,$con);
            if($result){
                if( mysql_affected_rows() > 0 ){
                    $status="success";
                }
            }else{
                $status="failed";
            }
            return $status;
}


  ?>

5 个答案:

答案 0 :(得分:1)

尝试echo喜欢

if($result){
    if( mysql_affected_rows() > 0 ){
        $status="success";
    }
} else {
    $status="failed";
}
return $status;

在你的if语句代码中,如

if($vals['type'] == "assign_amount")
{
    $values = assign_amount();  
    echo $values;
}

对于ajax返回目的,您最好echoprint而不是return

答案 1 :(得分:0)

最后在php文件中显示$status而不是return语句 你会得到警告

echo $status;

答案 2 :(得分:0)

你能试试吗,

    var locurl = 'dbentry.php';
    $.ajax({
        url: locurl,
        type:'post',
        data:param,
        dataType:'json',
        success:function(msg)
          {  
              alert(msg.status.sql);            
          }
    });

答案 3 :(得分:0)

要查看alert()消息,您必须阻止单击的提交按钮的默认行为:

$('#form-submit').click(function(e)
    {
        e.preventDefault();
        //....
    }

否则,将提交FORM并重新加载页面。

答案 4 :(得分:0)

你的代码有很多缺陷。例如,您正在连接字符串以创建数据对象。但如果有人在其中输入&=或任何其他特殊字符,您的表单就会失败。

您还可以对按钮上的单击功能进行绑定。虽然这有效,但对没有javascript的人来说没用。这可能不是问题,但可以通过一些小的改动轻松防止。

我会将<button name="submit"更改为<input type="submit",然后将jQuery绑定到自己的表单。同时将action属性添加到表单中以包含&#39; dbentry.php&#39;

$(function(){
  $('#contact-form').submit(function(){
    var $form = $(this);
    var data = $form.serialize();
    var locurl = 'dbentry.php';

    $.post(locurl,data, function(msg) {
      alert(msg.status)
    }, 'json');

   return false; //prevent regular submit
  });

});

现在要使它工作,PHP必须返回JSON数据。

<?php
header('Content-type: application/json');

//your code that includes
echo json_encode(array('status' =>$sql));  

//also notice that your code only returns data on success. Nothing on false.
?>