2012-01-15 27 views
2

私はフォームを提出すると(AJAXを使用して)クライアントの詳細が返されます。フォームフィールドはユーザーのIDを収集し、送信して結果を返します。テキストボックスのonchangeではなくsubmitボタンを使いたいのですが、テキストボックスの値を送信して何かを返すことができません。Ajaxフォーム送信ボタン付きで送信

これはシンプルにする必要があるようですが、もう少し手助けが必要です。

おかげで、 @rrfive

フォーム:

<form> 
<strong>Client ID:</strong> 
<input name="user" type="text" class="textBox" maxlength="10" /> 
<input type="submit" onclick="showUser(this.form); return false;"> 
</form> 

JS:

<script type="text/javascript"> 
function showUser(str) 
{ 
if (str=="") { 
    document.getElementById("txtHint").innerHTML=""; 
    return; 
    } 
if (window.XMLHttpRequest) { 
    xmlhttp=new XMLHttpRequest(); 
    } 
else { 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","includes/fetchUser.php?userID="+str,true); 
xmlhttp.send(); 
} 
</script> 

PHP:

<?php 
define('INCLUDE_CHECK',true); 
include 'config.php'; 

$userID=$_GET["userID"]; 
$sql="SELECT * FROM users WHERE ID = '".$userID."'"; 
$result = mysql_query($sql); 

$returned_rows = mysql_num_rows ($result); 

    if ($returned_rows == 0){ 
     echo '-- No results found --'; 
    } 
    else { 
     while($row = mysql_fetch_array($result)) { 
     echo "<div class='column'>"; 
     echo '<label>Name:</label>' . $row['lastName'] . ' 
     echo '</div>'; 
     } 
    } 

mysql_close($con); 
?> 

答えて

2

あなたが全体に渡す必要はありませんフォーム、ちょうど "ユーザー入力。

フォーム:あなたは、データベースクエリで使用する前に入力をサニタイズする必要があなたのPHPで

<form name="myform"> 
<strong>Client ID:</strong> 
<input name="user" type="text" class="textBox" maxlength="10" /> 
<input type="submit" onclick="showUser(document.myform.user.value); return false;"> 
</form> 

覚えていてください。 mysql_real_escape_stringまたはprepared statements

0
<form onsubmit="return false;"> 
+0

これはフォームが送信されないようにします。今、あなたはajaxをスムーズに呼び出すことができます... – Killswitch

関連する問題