大家好,如何获得通过 AJAX 和 PHP 发送数据时创建的自动递增 ID?

问题描述 投票:0回答:1

我希望通过SQL将buyer_id数据显示在另一个页面上。这就是我尝试从其他页面获取它的方法

<?php
include("bd_con.php");
$buyer-id= $_GET['buyer-id'];

echo "<script>alert('$buyer_id')</script>";
$query = "SELECT name, email, city, province, identification, address, extra-details, image, title, description, id_type 
          FROM `purchased_property` 
          inner join property ON purchased_property.property-id = property.id 
          WHERE buyer-id='$buyer-id'";

$linking = mysqli_query($bd_con, $query);
$info = mysqli_fetch_assoc($linking);
?>

这是我通过AJAX发送数据的页面的表单

<form class="payment-form" id="payment-form" method="POST">    
    <div class="form">
        <h2>Personal data</h2>
        <input type="text" placeholder="Full name" id="full_name" name="full_name"></input>

        <label>Date of birth</. label>
        <input type="date" id="date_birth" name="date_birth">

        <div id="paypal-button-container"></div>
    </div>
</form>

正如我向你们展示的那样,我尝试了

$_GET
,但没有成功。

javascript php mysql ajax ajaxform
1个回答
0
投票

您似乎正在尝试使用 AJAX 将 buyer_id 从一个页面传递到另一个页面,然后在 PHP 中使用 $_GET 检索它。但是,您的代码中存在一些问题可能会导致该问题。

在您的 AJAX 请求中,确保您将 buyer_id 作为参数发送。您可以通过将其附加到 URL 或将其作为数据负载的一部分发送来完成此操作。

在检索 buyer_id 的 PHP 代码中,请确保正在清理和验证输入以防止 SQL 注入。另外,检查变量名称是否正确。

请参阅下面提到的代码,我试图解决您面临的问题:

这是用于ajax请求的

$(document).ready(function() {
    $("#payment-form").submit(function(event) {
        event.preventDefault();

        // Get the buyer_id
        var buyer_id = // retrieve buyer_id from somewhere (e.g., AJAX 
        response or a variable)

        // Other form data
        var full_name = $("#full_name").val();
        var date_birth = $("#date_birth").val();

        // Your AJAX request
        $.ajax({
            type: "POST",
            url: "your_php_script.php",
            data: {
                buyer_id: buyer_id,
                full_name: full_name,
                date_birth: date_birth
                // Add other form fields as needed
            },
            success: function(response) {
                // Handle success
                console.log(response);
            },
            error: function(error) {
                // Handle error
                console.log(error);
            }
        });
    });
});

这是处理ajax请求的服务器端代码

<?php
include("bd_con.php");

// Sanitize and validate input
$buyer_id = isset($_POST['buyer_id']) ? mysqli_real_escape_string($bd_con, $_POST['buyer_id']) : '';

// Rest of your code
$query = "SELECT name, email, city, province, identification, address, extra-details, image, title, description, id_type 
          FROM `purchased_property` 
          INNER JOIN property ON purchased_property.property_id = property.id 
          WHERE buyer_id='$buyer_id'";

$linking = mysqli_query($bd_con, $query);
$info = mysqli_fetch_assoc($linking);

// Process $info as needed
?>

希望这对你有帮助

© www.soinside.com 2019 - 2024. All rights reserved.