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

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

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图