2013-09-02 3 views
0

양식을 가져야하는 데이터가 일부 있는데 아약스로 처리하려고합니다. 단추의 onclick 이벤트에 응답하는 함수가 있습니다. 버튼을 클릭하면 방화 광에서 일부 게시물 데이터가 있지만 PHP 스크립트에 도달하지 못합니다. 누가 잘못되었는지 알아?Ajax는 양식 데이터를 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); 
    }); 
} 

셋 결과 폼은 completly에 충전했을 때 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없이 잘 작동하지만 그냥 작동하지 않습니다.

귀하의 도움에 감사드립니다!

+0

'print_r ($ _ POST);'check under net> 응답 i n firebug –

+0

url : "/earnings.php", ' – DontVoteMeDown

+0

경로가 맞고 페이지가 PHP 스크립트로 이동하지 않고 새로 고침을하기 때문에 방화 광에서 PHP 응답을 볼 수 없습니다. html 형식. – Tom

답변

0

은 다음과 같이 양식 제출 및 사용 아약스를 방지 :

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

$('#createItem).on("submit",function(e){ 
e.preventDefault; 
newItem(); 
}); 
+0

그건 불행히도 작동하지 않았다. – Tom

+0

@Tom ok 내가 잘 말했듯이, 당신의 자바 스크립트가 들여다보기 전에 페이지가 다시로드되고있는 것처럼 들린다. html 폼을 게시하고 newItem();을 호출하는 방법을 보여 준다. – andrew

+0

@Tom ok 폼의 끝에 ''을 사용하는 주석의 내용을 보지 못했습니다. 그러면 return을 누르면 예상되는 양식이 제출됩니다. behavior – andrew

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

회신 나에게 당신이하려고하면 ...

+0

다음 오류가 발생했습니다 : "오류 게시 : [object Object]". 이것은 serializeArray() 및 serialize()에서도 마찬가지입니다. – Tom

+0

firebug ...와 을 사용하여 오류를 점검하십시오.이 ajax가 POST 할 때 PHP를 체크인 했습니까 ??? –

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> 
관련 문제