douzhi19900102 2013-10-08 12:57
浏览 70
已采纳

AJAX PHP变量填充表单字段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. 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. 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

Basically I dont want it to run the file_get_contents everytime my page loads because it is slow and not always needed. 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.

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.

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

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

  • 写回答

1条回答 默认 最新

  • dongyun3335 2013-10-08 13:10
    关注
    <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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮