不能通过post方法携带表单值

时间:2015-09-17 08:46:06

标签: php jquery mysql ajax forms

有一个表单,用户输入一个号码,并根据应用于该号码的条件,显示地址列表。我想存储通过AJAX返回的数据。页面上的代码具有以下形式:

的index.php

<script>
    $(document).ready(function() {
        $("#phone").keyup(function() {  
            var number = $("#phone").val();
            $.ajax({
                url: "t_fetchaddr.php",
                type: 'POST',
                data: 'number='+number,
                cache: false,
                }).done(function(html) { 
                    $('#results').html(html);
                });
            });
        });

<script>

<form action="insert_temp.php" method="POST">
    <input type="text" name="phoneno" id="phone" value=""  />
    <div id="results"></div>
    <button class="button btn btn-primary btn-large" type="submit" name="submit" value="submit">Submit</button>
</form>

t_fetchaddr.php页面上的代码

$val =  $_REQUEST['number']; 
$sql2 = "SELECT * FROM user_address where number='".$val."' ";
$result2 = mysqli_query($con, $sql2);
if (mysqli_num_rows($result2) > 0) 
{ ?>
    <div class="span6" >
        <div class="span3">
            <? while($row2 = mysqli_fetch_assoc($result2)) 
            { ?>
                <input type="radio" name="old_address" value="<? echo $row2['address']; ?>" ><? echo $row2['address']; ?><br>
            <? } ?>
        </div>
    </div>
<? } ?> 

insert_temp.php页面上的代码

$old_address = mysqli_real_escape_string($con, $_POST['old_address']);
echo $old_address;

一切正常,直到通过号码显示地址,但是当我提交表单时,它不会进入后端。我试着echo $old_address但却一无所获。 表单内索引页面中的其他输入值将转到后端,value that is being fetched from t_fetchaddr.php page is not getting carried,任何人都可以告诉我哪里出错了

3 个答案:

答案 0 :(得分:0)

<script>
    $(document).ready(function()
    {
        $("#phone").keyup(function()
        {
            var number = $("#phone").val();
            $.ajax({
                url: "t_fetchaddr.php",
                type: 'POST',
                data: {number :number}, //modified
                cache: false,
                success:function(html)
                {
                    $('#results').html(html);
                }
            });
        });
    });
</script>//Missing closing

<form action="insert_temp.php" method="POST">
    <input type="text" name="phoneno" id="phone" value=""  />
    <div id="results"></div>
    <button class="button btn btn-primary btn-large" type="submit" name="submit" value="submit" >Submit</button>
</form>

并在php中

$val =  $_POST['phoneno'];
$sql2 = "SELECT * FROM user_address where number='".$val."' ";
$result2 = mysqli_query($con, $sql2);
if (mysqli_num_rows($result2) > 0)
{ ?>
    <div class="span6" >
        <div class="span3">
            <? while($row2 = mysqli_fetch_assoc($result2))
            { ?>
                <input type="radio" name="old_address" value="<? echo $row2['address']; ?>" ><? echo $row2['address']; ?><br>
            <? } ?>
        </div>
    </div>
<? } ?> 
  

注意:

     
      
  1. 缺少结束标记</script>
  2.   
  3. 此行已更改data: 'number='+number,
  4.   

答案 1 :(得分:0)

试试这个并观看你的控制台:

$(document).ready(function() {
    $("#phone").keyup(function() {  
        var number = $(this).val();
        $.ajax({
            url: "t_fetchaddr.php",
            type: 'POST',
            data: {number:number},
            cache: false,
            success : function(html) { 
                $('#results').html(html);
            },
            error : function(err){
                console.log(err);
            }
        });
    });
});

答案 2 :(得分:0)

只需在fetchaddr.php上尝试此代码即可 我刚删除了php标签之间的内容。

<?
$val =  $_REQUEST['number']; 
$sql2 = "SELECT * FROM user_address where number='".$val."' ";
$result2 = mysqli_query($con, $sql2);
if (mysqli_num_rows($result2) > 0) { 
    echo '<div class="span6" >
        <div class="span3">';
            while($row2 = mysqli_fetch_assoc($result2)) 
            { 
               echo '<input type="radio" name="old_address" value="'.$row2['address'].'" >'.$row2['address'].'<br>';
             } 
        echo '</div>
    </div>';
} ?> 

希望这能解决您的问题。