2013-10-14 44 views
12

有问题任何人有任何想法?我只发布是代码所必需的。我基本上有一个HTML表单,我希望从表单中的字段获取值,然后再提交ajax调用并填充另一个字段。我认为如果我能够将该表单中输入的Txt转换为modcreate.php中的PHP变量,它将起作用。因为如果我手动输入没有变量的细节的话。如何通过AJAX调用将表单值传递给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

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

******解

相关部分*** *** 我的aja X呼叫缺少它发送的数据回表单字段

这里的部分是工作AJAX是什么样子,现在感谢你们所有的指针

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

是否modcreate.php给你任何结果,如果你直接去它? (例如:导航到modcreate.php?txt1 = somethinghere – Joao

+0

你必须定义你的AJAX方法'type:“POST”'或'type:“GET”' –

+1

@AdamZapp - jQuery默认为'GET' - 设置它 – 2013-10-14 21:00:11

回答

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> 

在服务器端:

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

然而,我只是通过添加$('#inputModel')。val(data);在我的ajax成功之后。我不知道它是否会与你提供的代码一起工作,但我会保持它incase我遇到问题与我工作的代码。除非你发布的方法比我的工作代码有优势吗? – user1548769

+0

注意缺少'$ field1value = $ income_data ['txt1']''代替最后一个''''末尾的分号 – thefoxrocks

+0

@ McLean25谢谢,修正 –