如何通过AJAX调用将表单值传递给PHP变量? [英] How to pass form value to PHP variable with AJAX call?

查看:146
本文介绍了如何通过AJAX调用将表单值传递给PHP变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到问题,有人有任何想法吗?我只张贴了必要的代码.我基本上有一个HTML表单,我想在提交表单之前从表单上的字段中提取值,然后运行ajax调用并填充另一个字段.我认为,如果我可以将以表格形式输入的TXT传递到modcreate.php上的PHP变量,它将可以正常工作.因为如果我手动输入没有变量的详细信息,它将起作用.

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

mainpage.php

表单中的相关部分

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

  <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

modcreate.php

<?php 

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

******解决方案****** 我的ajax调用缺少将数据发送回表单字段的部分

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

这是工作的ajax现在的样子,谢谢大家的指点

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>

推荐答案

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

在服务器端:

$income_data = json_decode($_GET['data'], true);
$field1value = $income_data['txt1'];

这篇关于如何通过AJAX调用将表单值传递给PHP变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆