Ajax表单提交提交按钮

时间:2022-04-09 08:38:09

I've got form which when submitted (using AJAX) returns a bunch of client details. The form field collects the user's ID, submits and returns the results. I'd like to use a submit button rather than an onchange on the textbox but am unable to submit the textbox value and have the anything returned.

我有一个表单,当提交(使用AJAX)返回一堆客户端详细信息。表单字段收集用户的ID,提交并返回结果。我想在文本框上使用提交按钮而不是onchange但是无法提交文本框值并返回任何内容。

This seems like it should be simple but I'm again in need of some help.

这似乎应该很简单,但我又需要一些帮助。

Thanks, @rrfive

Form:

<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 个解决方案

#1


2  

You don't need to pass in the whole form, just the "user" input.

您不需要传递整个表单,只需传入“用户”输入。

Form:

<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>

In your PHP, please remember you need to sanitise the input before using it in a database query. Read up on mysql_real_escape_string or prepared statements

在PHP中,请记住在数据库查询中使用它之前需要清理输入。阅读mysql_real_escape_string或预处理语句

#2


0  

<form onsubmit="return false;">

#1


2  

You don't need to pass in the whole form, just the "user" input.

您不需要传递整个表单,只需传入“用户”输入。

Form:

<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>

In your PHP, please remember you need to sanitise the input before using it in a database query. Read up on mysql_real_escape_string or prepared statements

在PHP中,请记住在数据库查询中使用它之前需要清理输入。阅读mysql_real_escape_string或预处理语句

#2


0  

<form onsubmit="return false;">