用于填充表单字段 jquery 的 AJAX PHP 变量

AJAX PHP variable to populate form field jquery

本文关键字:AJAX PHP 变量 jquery 填充 表单 字段 用于      更新时间:2023-09-26

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

场景:我有一个 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]";
?>

我需要一些东西来允许我用测试中的 php 变量$print填充表单字段.php并且仅在单击字段时才将其放入 inputTag 字段的值中

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

当涉及到这些东西时,我是一个菜鸟,但可以绕过一些......我能弄清楚的最好的是我需要用这样的东西进行 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文件中,你应该只输出/回显出你想要插入到输入字段中的字符串/值