最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass form value to PHP variable with AJAX call? - Stack Overflow

programmeradmin4浏览0评论

Having issues anyone has any ideas? I have only posted was is necessary for the code. I basically have an HTML form I want to pull the value from a field on a form before it's submitted run an ajax call and populate another field. I think if I could get that Txt that is entered in the form over to a PHP variable on the modcreate.php it will work. because if I manually enter the details without the variable it works.

mainpage.php

Relevant parts from form

  <tr>
    <th>Service Tag:</th>
    <th><input type="text" id="inputTag" name="inputTag" value="" onblur="this.value=this.value.toUpperCase()"></td>
  </tr>

and

  <tr>
    <th>Model:</th>
    <th>
       <input type="text" id="inputModel" name="inputModel" value=""> 
       <a href="#" id="updateBtn">Populate</a>
       <div id="spinner">
          <img src="\images\ajax-load.gif" alt="Loading..."/>
       </div>
    </th>
  </tr>


 <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: "modcreate.php",
       data: { 'txt1': $('#inputTag').val() },
       success: function(data) {
       }
    });
 });
 </script>

modcreate.php

<?php 

$field1value = $_GET['txt1'];
    $file_string = file_get_contents('blahblah/'.$field1value);
    preg_match("/<title>Product Support for (.+)\| Dell/i", $file_string, $matches);
    $print = "$matches[1]";
    echo $print;
?>

******Solution****** my ajax call was missing the part where it sent the data back to the form field

here is what the working ajax looks like now thanks guys for all the pointers

 <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: "modcreate.php",
         data: { 'txt1': $('#inputTag').val() },
         success: function(data) {
            $('#inputModel').val(data); //this was the missing part
         }
     });
 });
</script>

Having issues anyone has any ideas? I have only posted was is necessary for the code. I basically have an HTML form I want to pull the value from a field on a form before it's submitted run an ajax call and populate another field. I think if I could get that Txt that is entered in the form over to a PHP variable on the modcreate.php it will work. because if I manually enter the details without the variable it works.

mainpage.php

Relevant parts from form

  <tr>
    <th>Service Tag:</th>
    <th><input type="text" id="inputTag" name="inputTag" value="" onblur="this.value=this.value.toUpperCase()"></td>
  </tr>

and

  <tr>
    <th>Model:</th>
    <th>
       <input type="text" id="inputModel" name="inputModel" value=""> 
       <a href="#" id="updateBtn">Populate</a>
       <div id="spinner">
          <img src="\images\ajax-load.gif" alt="Loading..."/>
       </div>
    </th>
  </tr>


 <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: "modcreate.php",
       data: { 'txt1': $('#inputTag').val() },
       success: function(data) {
       }
    });
 });
 </script>

modcreate.php

<?php 

$field1value = $_GET['txt1'];
    $file_string = file_get_contents('blahblah./'.$field1value);
    preg_match("/<title>Product Support for (.+)\| Dell/i", $file_string, $matches);
    $print = "$matches[1]";
    echo $print;
?>

******Solution****** my ajax call was missing the part where it sent the data back to the form field

here is what the working ajax looks like now thanks guys for all the pointers

 <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: "modcreate.php",
         data: { 'txt1': $('#inputTag').val() },
         success: function(data) {
            $('#inputModel').val(data); //this was the missing part
         }
     });
 });
</script>
Share Improve this question edited Oct 17, 2017 at 8:00 Matt Komarnicki 5,4428 gold badges48 silver badges97 bronze badges asked Oct 14, 2013 at 20:54 user1548769user1548769 3193 gold badges5 silver badges12 bronze badges 8
  • Is modcreate.php giving you any results if you go to it directly? (ex: navigate to modcreate.php?txt1=somethinghere – Joao Commented Oct 14, 2013 at 20:57
  • You have to define Your AJAX method type:"POST" or type:"GET" – Adam Azad Commented Oct 14, 2013 at 20:58
  • 1 @AdamZapp - jQuery defaults to GET - it's not necessary to set it. – user1864610 Commented Oct 14, 2013 at 21:00
  • Please define 'Having Issues' – user1864610 Commented Oct 14, 2013 at 21:00
  • 1 What specific problem are you having? One thing I see missing is the form submit, which you would presumably want to call from within your ajax success function. – mayabelle Commented Oct 14, 2013 at 21:02
 |  Show 3 more ments

1 Answer 1

Reset to default 1
<script type="text/javascript"> 
 $('#updateBtn').click(function(e){
    //to disable the click from going to next page
    e.preventDefault();
    $.ajax({
            url: "modcreate.php",
            data: 'data="{ "txt1": '+$('#inputTag').val()+' }"',
            success: function(data){


          }
     }
);
});
</script>

On server side:

$ine_data = json_decode($_GET['data'], true);
$field1value = $ine_data['txt1'];
发布评论

评论列表(0)

  1. 暂无评论