【发布时间】:2013-10-22 13:03:27
【问题描述】:
有问题有人有什么想法吗?我只发布了代码是必需的。我基本上有一个 HTML 表单,我想在提交之前从表单上的字段中提取值,然后运行 ajax 调用并填充另一个字段。我想如果我可以将表单中输入的 Txt 转移到 modcreate.php 上的 PHP 变量中,它将起作用。因为如果我手动输入没有变量的详细信息,它就可以工作。
mainpage.php
表格中的相关部分
<tr>
<th>Service Tag:</th>
<th><input type="text" id="inputTag" name="inputTag" value="" onblur="this.value=this.value.toUpperCase()"></td>
</tr>
和
<tr>
<th>Model:</th>
<th>
<input type="text" id="inputModel" name="inputModel" value="">
<a href="#" id="updateBtn">Populate</a>
<div id="spinner">
<img src="\images\ajax-load.gif" alt="Loading..."/>
</div>
</th>
</tr>
<script type="text/javascript" src="\js\jquery-latest.js"></script>
<script type="text/javascript">
$('#updateBtn').click( function(e) {
//to disable the click from going to next page
e.preventDefault();
$.ajax({
url: "modcreate.php",
data: { 'txt1': $('#inputTag').val() },
success: function(data) {
}
});
});
</script>
modcreate.php
<?php
$field1value = $_GET['txt1'];
$file_string = file_get_contents('blahblah.com/'.$field1value);
preg_match("/<title>Product Support for (.+)\| Dell/i", $file_string, $matches);
$print = "$matches[1]";
echo $print;
?>
******解决方案****** 我的 ajax 调用缺少将数据发送回表单字段的部分
这就是现在工作的 ajax 的样子,感谢大家的指点
<script type="text/javascript" src="\js\jquery-latest.js"></script>
<script type="text/javascript">
$('#updateBtn').click(function(e){
//to disable the click from going to next page
e.preventDefault();
$.ajax({
url: "modcreate.php",
data: { 'txt1': $('#inputTag').val() },
success: function(data) {
$('#inputModel').val(data); //this was the missing part
}
});
});
</script>
【问题讨论】:
-
如果你直接去modcreate.php,它会给你任何结果吗? (例如:导航到 modcreate.php?txt1=somethinghere
-
你必须定义你的AJAX方法
type:"POST"或type:"GET" -
@AdamZapp - jQuery 默认为
GET- 没有必要设置它。 -
请定义“有问题”
-
您遇到了什么具体问题?我看到缺少的一件事是表单提交,您可能想从您的 ajax 成功函数中调用它。
标签: javascript php ajax forms