简体   繁体   中英

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

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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