Cannot transfer form values ​​via post method

There is a form in which the user enters a number and, according to the condition that applies to the number, a list of addresses is displayed. I would like to save data that is returned via AJAX. The code on the page, which has the form:

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>

on t_fetchaddr.php page

$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>
<? } ?> 

code on insert_temp.php

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

Everything works fine until the address is displayed by number, but when I submit the form, it does not go to the background. I tried echo $old_address, but received nothing. The other input values ​​on the index page inside the form are going to the backend, but value that is being fetched from t_fetchaddr.php page is not getting carriedcan anyone tell me where I made a mistake.

+4
source share
3
<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>
<? } ?> 

:

  • </script>
  • data: 'number='+number,
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);
            }
        });
    });
});
0

just try this code on fetchaddr.php I just deleted between php tags.

<?
$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>';
} ?> 

hope this solves your problem.

0
source

Source: https://habr.com/ru/post/1607778/


All Articles