2014-11-05 96 views
0

我對AJAX相當陌生,它從來沒有真正解決過,但我最終想要解決它。這就是爲什麼我要求你們幫忙!多年來,在閱讀了數百個答案之後,我終於明白了我無法解決的問題。Ajax和PHP提交

我有一個顯示一個表單填寫一個簡單的jQuery UI彈出:

<form> 
 
<div id="Dialog2"> 
 
      <p>Nieuwe Klant</p> 
 
      <table width="100%" border="0"> 
 
      <tr> 
 
       <th scope="row">Naam:</th> 
 
       <td><input name="nc_name" type="text" id="nc_name" tabindex="1" /></td> 
 
      </tr> 
 
      <tr> 
 
       <th scope="row">Type:</th> 
 
       <td><select name="nc_type" id="nc_type" tabindex="2"> 
 
       <option>Bedrijf</option> 
 
       <option>Particulier</option> 
 
       </select></td> 
 
      </tr> 
 
      <tr> 
 
       <th scope="row">Adres:</th> 
 
       <td><input name="nc_adres" type="text" id="nc_adres" tabindex="3" /></td> 
 
      </tr> 
 
      <tr> 
 
       <th scope="row">Postcode:</th> 
 
       <td><input name="nc_zip" type="text" id="nc_zip" tabindex="4" /></td> 
 
      </tr> 
 
      <tr> 
 
       <th scope="row">Land:</th> 
 
       <td><input name="nc_country" type="text" id="nc_country" tabindex="5" /></td> 
 
      </tr> 
 
      <tr> 
 
       <th scope="row">Stad:</th> 
 
       <td><input name="nc_city" type="text" id="nc_city" tabindex="6" /></td> 
 
      </tr> 
 
      <tr> 
 
       <th scope="row">KVK Nummer:</th> 
 
       <td><input name="nc_kvk" type="text" id="nc_kvk" tabindex="8" /> <input name="kvkzoek" type="button" id="kvkzoek" tabindex="7" value="Zoek Automatisch" onclick="seeKVKnumber()" /></td> 
 
      </tr> 
 
      <tr> 
 
       <th scope="row"><div id="info" /></th> 
 
       <td><input name="submit_two" type="button" id="submit_two" tabindex="9" value="Klant Toevoegen" /></td> 
 
      </tr> 
 
      </table> 
 
      <p><br /> 
 
      </p> 
 
     </div> 
 
     </form>

這是由下面這段腳本在同一頁面的頁眉處理:

$(document).ready(function(){ 
        $("#submit_two").click(function(){ 

          var name=$("#nc_name").val(); 
          var type=$("#nc_type").val(); 
          var adres=$("#nc_adres").val(); 
          var zip=$("#nc_zip").val(); 
          var country=$("#nc_country").val(); 
          var city=$("#nc_city").val(); 
          var kvk=$("#nc_kvk").val(); 

          $.ajax({ 
           type:"get", 
           url:"addcustomer.php", 
           data:"name="+name+"&type="+type+"&adres="+adres+"&zip="+zip+"&country="+country+"&city="+city+"&kvk="+kvk, 
           success:function(data){ 
           $("#info").html(data); 
           } 

          }); 

        }); 
       }); 

我已經使用「POST」以及「GET」,但都不起作用。 處理該INSERT到我的數據庫PHP頁面看起來是這樣的:

<? 
include('../includes/dbconfig.php'); 

    $name=$_GET["name"]; 
    $type=$_GET["type"]; 
    $adres=$_GET["adres"]; 
    $zip=$_GET["zip"]; 
    $city=$_GET["city"]; 
    $kvk=$_GET["kvk"]; 
    $country=$_GET["country"]; 

    //Insert query 
    $query = mysqli_query($con,"insert into customers(name, address, zipcode, city, kvk, country) values ('$name','$type','$adres','$zip','$city','$kvk','$country')"); 
if($query){ 
    echo "Your comment has been sent"; 
    } 
    else{ 
    echo "Error in sending your comment"; 
    var_dump($_GET); 
    } 
mysqli_close($con); // Connection Closed 
?> 

每次我填寫表格,我得到了錯誤:「錯誤的發送您的評論。」

即使後續代碼var_dump($ _ GET)顯示正確的價值觀:

array(7) { ["name"]=> string(5) "MrBla" ["type"]=> string(7) "Bedrijf" ["adres"]=> string(6) "Blabla" ["zip"]=> string(7) "aaaabla" ["country"]=> string(6) "blabla" ["city"]=> string(3) "bla" ["kvk"]=> string(3) "bla" } 

但值只是不會出現在數據庫中。 Mozilla的Firebug也不會顯示任何錯誤或中斷。

我希望你們中的任何一位能夠幫助我避開這些問題?

預先感謝大家,並感謝大家多年來爲他人提供的許多有用答案。

+0

它與ajax調用無關,它的插入到查詢中有一個錯誤,它的字段列表中缺少「type」字段。而且,如果您在進行測試時使用'mysqli_error'或'$ mysqli-> error'而不是自定義錯誤消息,那麼它會更好,因此您可以看到返回的確切錯誤。 – Thanu 2014-11-05 11:47:20

回答

1

您的SQL語法錯誤。 我認爲這是INSERT INTO語句中列的不同數量。 請從VALUES部分刪除$type,或在COLUMN定義部分添加type

下次如果您有類似問題,請發送mysqli_error輸出。 :)

+0

刪除值可能不是OP所需的結果。你應該聲明他需要在他的查詢中添加「type」列。 也嘗試標記你的答案 – DarkBee 2014-11-05 11:49:53

+0

感謝您的信息。但我寫道,這將是另一種添加'type'列的解決方案。 – Giwwel 2014-11-05 12:14:54

+0

似乎我重讀那部分。我是sry :) – DarkBee 2014-11-05 12:16:00

1

你忘了你的查詢添加列type

$query = mysqli_query($con,"insert into customers(name, address, zipcode, city, kvk, country) values ('$name','$type','$adres','$zip','$city','$kvk','$country')"); 

應該

$query = mysqli_query($con,"insert into customers(name, type, address, zipcode, city, kvk, country) values ('$name','$type','$adres','$zip','$city','$kvk','$country')"); 
  • 避免SQL注入通過轉義的變量。
+0

它盯着我的臉,我沒有看到它......非常感謝你們! – 2014-11-05 12:30:31