gpt4 book ai didi

javascript - AJAX PHP变量来填充表单字段jquery

转载 作者:搜寻专家 更新时间:2023-10-31 22:05:33 25 4
gpt4 key购买 nike

尝试找到解决方案任何帮助或指示让我朝着正确的方向前进,我们将不胜感激。

场景:我有一个 HTML 表单,用于通过 PHP 修改 mysql 数据库。我希望当页面加载时,它在此表单上有一个从数据库中提取的字段,这很容易

<input type="text" name="inputTag" value="<?php echo $people['Service Tag']; ?>"     onblur="this.value=this.value.toUpperCase()" />

那太简单了。我有一个单独的 php 页面说... test.php 从页面上刮下文本并存储在变量中。与此类似的东西

<?php 
$file_string = file_get_contents('http://blahblah.com/');
preg_match("/<title>Product Support for (.+)\| HP/i", $file_string, $matches);
$print = "$matches[1]";
?>

我需要一些东西,允许我使用 test.php 中的 php 变量 $print 填充表单字段,并仅在单击该字段时将其放入 inputTag 字段的值

基本上我不希望它在我的页面每次加载时都运行 file_get_contents,因为它很慢而且并不总是需要。但我希望表单字段显示当前存储在数据库 onload 中的内容,但允许您单击触发屏幕抓取 php 文件的表单字段,并在表单的文本字段中回显该 php 文件中的 $print 变量,以便我可以在填写完表格后提交表格。

当涉及到这些东西时,我有点菜鸟,但可以绕过一些......我能想到的最好的是我需要用这样的东西进行 AJAX 调用。

<script type="text/javascript" src="\js\jquery-latest.js"></script>
<script type="text/javascript">
function populatetag() {
$.get("\portable\test.php");
return false;
}
</script>

像往常一样加载初始页面,从数据库中提取当前信息。然后可以添加一些带有 onclick 事件的 javascript 来清除字段并使用来自 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: "test.php",
success: function(data){
//set the value of your input field with the data received
$('#model').val(data);
}
}
);
});
</script>

这是我最终用来让它工作的方法,谢谢

最佳答案

<input type="text" id="serviceTag" name="inputTag" value="<?php echo $people['Service Tag']; ?>"/>
<a href="#" id="updateBtn">Update Field</a>

<script>
$('#updateBtn').click(function(e){
//to disable the click from going to next page
e.preventDefault();
$.ajax(
'your-php-script.php', function(data){
//set the value of your input field with the data received
$('#serviceTag').val(data);
}
);
});
</script>

在您的 php 文件中,您应该只输出/回显要插入到输入字段中的字符串/值

关于javascript - AJAX PHP变量来填充表单字段jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19248375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com