2011-12-11 84 views
1

我正在嘗試提交表單並使用php進行處理。這裏是我做的(它簡化,但仍然沒有工作...)無法使用Ajax提交表單

這裏我的索引:

<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <script type="text/javascript" src="jquery-1.7.1.min.js"></script> 
    <script> 
     $().ready(function(){ 
     $("#addemail").click(function(){ 
     var email=$("#email").val(); 
     $.ajax({ 
      type: "POST", 
      url: "addemail.php", 
      data: "email="+email, 
      success: function(){ 
       console.log("success! ") 
       }, 
      error: console.log("error!! ")  
      });   
     }); 
    }); 
    </script> 
</head> 
<body>   
    <div id="wrapper">   
      <h2>Mon form...</h2> 
      <form action=""> 
       <table> 
       <tr><td><label>Email:</label></td> 
        <td><input type="text" id="email" name="email" /></td> 
        <td><input type="submit" id="addemail" value="Add" /></td> 
       </tr> 
       </table>     
      </form>    
    </div> 
</body> 

這是我的PHP文件:

<?php 
$connection = mysql_connect('localhost', 'XXX', 'XXX'); 
$db= mysql_select_db('mydb', $connection); 
$email= $_REQUEST["email"]; 
$query = "INSERT INTO users(email) VALUES ('$email')"; 
mysql_query($query); 

>

我在做什麼錯誤(可能不是PHP,因爲如果我在我的表單中給它起作用, php not ajax ...) Thanx提前

+1

定義'not working'。你有錯誤嗎?控制檯告訴你什麼? – PeeHaa

+2

您有一個SQL注入漏洞。 – SLaks

+0

@PeeHaa:呃,是的,我只有'錯誤!!'登錄控制檯...我怎樣才能更好地調試jQuery? – blop

回答

1
$email= $_REQUEST["email"]; 
$query = "INSERT INTO users(email) VALUES ('$email')"; 
mysql_query($query); 

SQL注入漏洞!

此外,你不需要添加分號嗎?

success: function(){ 
    console.log("success!"); 
}, 

並把錯誤內的一個匿名函數?

error: function(){ 
    console.log("error!"); 
} 

而且您的形式應該是儘可能全面:

<form id="formContact" method="post" action="addemail.php"> 
    <input type="text" name="email" id="email" /> 
    <input type="submit" id="addemail" value="Add"/> 
</form> 
+0

爲什麼'stripslashes'? – SLaks

+0

http://stackoverflow.com/questions/1522313/php-mysql-real-escape-string-stripslashes-leaving-multiple-slashes如果魔術引號打開,那麼你可能會以可怕的\\斜槓鏈結束。 –

+0

Thanx,但如果我做的行動=「addemail.php」et在這個頁面上(這是空白的...),並不執行ajax ...爲什麼? – blop

0

試一試,我用這個,看看你的代碼在你的表單中看起來像你的你的方法= post,也在你的php中使用$ _POST沒有請求..

$(document).ready(function() { 
     $("#formContact").submit(function(event) { 
     /* stop form from submitting normally */ 
     //event.preventDefault(); 
     $.post("sendemail.php", $("#formContact").serialize()); 
     return false; 
    }); 
}); 


<form id="formContact" method="post" action="/"> 
<input type="text" name="name" id="name" /> 
<input type="submit" name="submit" value="send"/> 
</form> 
+0

那麼,有改進,但仍然不工作,因爲我想...如果我把PHP的形式的目標,它會在這個頁面上,而不是執行Ajax ...當我不給任何動作,現在它寫入數據庫但我看不到我的日誌,當我把一個斷點後,阿賈克斯... – blop

0

我不知道這是否是問題,但你可以給一個嘗試.. document在準備發言misssing,也給散列格式的數據,你也應該寫一個錯誤的函數。請嘗試以下代碼:

<script type="text/javascript"> 
     $(document).ready(function(){ 
     $("#addemail").click(function(){ 
     var email=$("#email").val(); 
     $.ajax({ 
      type: "POST", 
      url: "addemail.php", 
      data: { 
      email:email 
      }, 
      success: function(){ 
       console.log("success! ") 
       }, 
      error: function(){ 
       console.log("error!! ") } 
      });   
     }); 
    }); 
    </script>