gpt4 book ai didi

javascript - PHP 无法正确接收我表单中的数据

转载 作者:行者123 更新时间:2023-12-01 03:20:40 24 4
gpt4 key购买 nike

我正在使用 Ajax 将表单的值发送到我的 php 文件并将其插入到我的数据库中,具体取决于按钮的操作,它将执行 url,在本例中是 insert_shirt.php,但它似乎不起作用,因为它不会发送表单数据,就像表单完全为空一样

HTML

<form method="post" id="form_shirt">
ID:
<br>
<input type="hidden" name="id_shirt" id="id_shirt" class="form-control"> Name:
<br>
<input type="text" name="name_shirt" id="name_shirt" class="form-control" required="required"> Price:
<br>
<input type="text" name="price_shirt" id="price_shirt" class="form-control" required="required">

<button id="insert_shirt" class="submit" name="btninsert">INSERT</button>

</form>

JavaScript

$(document).ready(function() {
$('.submit').on("click", function() {
$.ajax({
url: this.id + ".php",
method: "POST",
data: $('#form_shirt').serialize(),
contentType: false,
cache: false,
processData: false,
success: function(data) {
$('#form_shirt')[0].reset();
$('#table_shirt').html(data);
}
});
});
});

PHP

<?php

$connect = mysqli_connect("localhost", "root", "", "shirts");

$output = '';
$name_shirt = mysqli_real_escape_string($connect, $_POST["name_shirt"]);
$price_shirt = mysqli_real_escape_string($connect, $_POST["price_shirt"]);

$query = "INSERT into shirts ( name, price)
VALUES ('$name_shirt','$price_shirt') ";
if(mysqli_query($connect, $query))
{
$output .= '<label class="text-success">Data Inserted</label>';
$select_query = "SELECT id_shirt, name, price FROM shirts";
$result = mysqli_query($connect, $select_query);
$output .= '
<table id="shirts" class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>PRICE</th>
</tr>
</thead>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<tbody>
<td>' . $row["id_shirt"] . '</td>
<td>' . $row["name"] . '</td>
<td>' . $row["price"] . '</td>
</tr>
</tbody>
';
}
$output .= '</table>';
}
echo $output;

?>

最佳答案

设置 contentType: "application/x-www-form-urlencoded; charset=UTF-8" 如果您未将其包含在请求中,则这是默认值。您将其设置为 false,这可能就是您的 $_POST 看不到表单数据的原因。

关于javascript - PHP 无法正确接收我表单中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45227390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com