简体   繁体   English

AJAX PHP变量填充表单字段jquery

[英]AJAX PHP variable to populate form field jquery

Trying to find a solution any help or pointers to get me in the right direction would be appreciated. 试图寻找解决方案任何帮助或指示,让我朝着正确的方向,将不胜感激。

Scenario: I have a HTML form that I use to modify a mysql database with PHP. 场景:我有一个HTML表单,我用它来修改PHP的mysql数据库。 I want when the page loads it has a field on this form that pulls from the database which is easy enough with 我想在页面加载时,它在这个表单上有一个字段,从数据库中提取,这很容易

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

That would be to easy. 这很容易。 I have a separate php page say... test.php that scrapes text off of a page and stores in a variable. 我有一个单独的php页面说... test.php,它从页面中删除文本并存储在变量中。 With something similar to this 与此类似的东西

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

I need something that will allow me to populate the form field with the php variable $print from the test.php and put that into the value of the inputTag field only if the field is clicked 我需要的东西允许我用test.php中的php变量$ print填充表单字段,并且只有在单击该字段时才将其放入inputTag字段的值中

Basically I dont want it to run the file_get_contents everytime my page loads because it is slow and not always needed. 基本上我不希望它每次我的页面加载时都运行file_get_contents,因为它很慢而且并不总是需要。 But I want the form field to display what is currently stored in the database onload but allow you to click that form field which triggers the screen scrape php file and echos the $print variable from that php file in the text field of the form so that I can then submit the form after im finished filling it out. 但我希望表单字段显示当前存储在数据库onload中的内容,但允许您单击该窗体字段,触发屏幕刮取php文件并从该窗体的文本字段中的该php文件中回显$ print变量,以便我可以在完成填写后提交表格。

I am kind of a noob when it comes to this stuff but can get around some... Best I could figure out is that I need to make a AJAX call with something like this. 当谈到这个东西时,我就像一个菜鸟,但可以绕过一些......最好的我能弄明白的是我需要用这样的东西进行AJAX调用。

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

Load the initial page as usually which pulls the current info from the database. 通常加载初始页面,从数据库中提取当前信息。 Then maybe add some javascript with a onclick event that clears the field and populates it with the variable from the ajax call? 那么也许可以添加一些带有onclick事件的javascript来清除字段并使用ajax调用中的变量填充它?

Is this even possible or am I living in a dreamland? 这甚至可能还是我生活在梦境中? sorry if this is confusing I can try to clarify if needed. 抱歉,如果这令人困惑我可以尝试澄清,如果需要。

thanks! 谢谢!

UPDATE * Update Field 更新 *更新字段

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

this is what I finally used to to get it to work thanks 这是我最终用它来工作的感谢

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

In your php file, you should output/echo out just that string/value that you want to be inserted into the input field 在您的php文件中,您应该输出/ echo出您想要插入到输入字段中的字符串/值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM