0

我得到了一个表格,里面有一些需要发送的数据,我想用 ajax 来做。我有一个响应按钮的 onclick 事件的函数。当我单击按钮时,我在 firebug 中获得了一些发布数据,但它没有到达我的 PHP 脚本。有谁知道出了什么问题?

JS:

function newItem() {
var dataSet = $("#createItem :input").serialize();


confirm(dataSet); //Below this code box is the output of this variable to check whether it is filled or not

    var request = $.ajax({
        type: "POST",
        url: "/earnings.php",
        data: dataSet,
        dataType: "json"
    });

    request.done(function(){
        $('.create_item').children(".row").slideUp('100', 'swing');
        $('.create_item').children("h2").slideUp('100', 'swing');
        confirm("succes");
    });

    request.fail(function(jqXHR, textStatus) {
        confirm( "Request failed:" + textStatus );
    });
}

表格填写完毕后的dataSet结果: id=123&date=13-09-2013&amount=5&total=6%2C05&customer=HP&invoicenumber=0232159&quarter=1&description=Test

PHP:

<?php 

    include('includes/dbconn.php');

    function clean_up($string){
        $html = htmlspecialchars($string);
        $string = mysql_real_escape_string($html);
        return $string;
    }

    if($_POST){

        $date = clean_up($_POST['date']);
        $amount = clean_up($_POST['amount']);
        $total = clean_up($_POST['total']);
        $customer = clean_up($_POST['customer']);
        $invoicenumber = clean_up($_POST['invoicenumber']);
        $quarter = clean_up($_POST['quarter']);
        $description = clean_up($_POST['description']);

        $sql = ("INSERT INTO earnings (e_date, e_amount, e_total, e_customer, e_invoicenumber, e_quarter, e_description) 
            VALUES ($date, '$amount', '$total', '$customer', $invoicenumber, $quarter, '$description')");
        echo $sql;
        if($mysqli->query($sql) === true){
            echo("Successfully added");
        }else{
            echo "<br /> \n" . $mysqli->error;
        }
    }
?>

没有 ajax 的表单可以正常工作,但有了它就行不通了。

感谢您的帮助!

4

3 回答 3

1

试试这个片段代码兄弟...

<form id="F_login">
    <input type="text" name="email" placeholder="Email">
    <input type="password" name="password" placeholder="Password">
    <button id="btn_login" type="submit">Login</button>
</form>

$("#btn_login").click(function(){
    var parm = $("#F_login").serializeArray();
    $.ajax({
        type: 'POST',
        url: '/earnings.php',
        data: parm,
        success: function (data,status,xhr) {
            console.info("sukses");
        },
        error: function (error) {
            console.info("Error post : "+error);
        }
    });
});

如果你试试这个,请回复我...

于 2013-09-03T12:11:30.913 回答
0

阻止您的表单提交并像这样使用 ajax:

<form id="createItem">
<input id="foo"/>
<input id="bar"/>
<input type="submit" value="New Item"/>
</form>

$('#createItem).on("submit",function(e){
e.preventDefault;
newItem();
});
于 2013-09-02T20:57:28.807 回答
0

尝试这个

<input type="text" id="foo"/>
<input type="text"  id="bar"/>
<input type="button" id="btnSubmit" value="New Item"/>


<script type="text/javascript">
    $(function() {
$("#btnSubmit").click(function(){
try
  {
    $.post("my php page address",
      {
         'foo':$("#foo").val().trim(),
         'bar':$("#bar").val().trim()
     }, function(data){
           data=data.trim();
      //   alert(data);
// this  data is data that the server sends back in case of ajax call you 
//can send any type of data whether json or json array or any other type 
//of data

         });
    }
    catch(ex)
    {
        alert(ex);
    }
  });


});
</script>
于 2015-03-04T18:48:42.860 回答