[英]Why input from the form is not being passed
I am new to php and have created two forms. 我是php新手,创建了两种形式。 One form takes in the user's first name and the second takes in the user's last name.
一种形式采用用户的名字,第二种形式采用用户的名字。 I am not sure why that when the user inputs text into the forms nothing is being passed.
我不确定为什么当用户在表单中输入文本时没有传递任何内容。
The php with the forms PHP的形式
<?php include '../view/header.php'; ?>
<div id="main">
<h1>Add Athlete</h1>
<form action="index.php" method="post" id="add_product_form">
<input type="hidden" name="action" value="add_product" />
<label>Country:</label>
<select name="category_id">
<?php foreach ( $categories as $category ) : ?>
<option value="<?php echo $category['categoryID']; ?>">
<?php echo $category['categoryName']; ?>
</option>
<?php endforeach; ?>
</select>
<br />
<label>Code:</label>
<input type="input" name="code" />
<br />
<label>First Name:</label>
<input type="input" name="first_name" />
<br />
<label>Last Name:</label>
<input type="input" name="last_name" />
<br />
<label> </label>
<input type="submit" value="Add Athlete" />
<br /> <br />
</form>
<p><a href="index.php?action=list_products">View List of Olympic Athletes</a></p>
</div>
<?php include '../view/footer.php'; ?>
The php that takes in input from the forms 从表单接受输入的php
<?php
require('../model/database.php');
require('../model/product_db.php');
require('../model/category_db.php');
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'list_products';
}
if ($action == 'list_products') {
// Get the current category ID
$category_id = $_GET['category_id'];
if (!isset($category_id)) {
$category_id = 1;
}
// Get product and category data
$category_name = get_category_name($category_id);
$categories = get_categories();
$products = get_products_by_category($category_id);
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
} else if ($action == 'show_add_form') {
$categories = get_categories();
include('product_add.php');
} else if ($action == 'add_product') {
$category_id = $_POST['category_id'];
$first_name = "";
if(isset($_POST['first_name'])){$first_name = $_POST['FirstName'];}
$last_name = "";
if(isset($_POST['last_name'])){$last_name = $_POST['LastName'];}
// Validate the inputs
if (empty($first_name) || empty($last_name)) {
$error = "Invalid product data. Check all fields and try again.";
include('../errors/error.php');
} else {
add_product($category_id, $first_name, $last_name);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
}
} else if ($action == 'list_categories') {
$categories = get_categories();
include('category_list.php');
} else if ($action == 'add_category') {
$first_name = $_POST['FirstName'];
// Validate inputs
if (empty($name)) {
$error = "Invalid category name. Check name and try again.";
include('view/error.php');
} else {
add_category($name);
header('Location: .?action=list_categories'); // display the Category List page
}
} else if ($action == 'delete_category') {
$category_id = $_POST['category_id'];
delete_category($category_id);
header('Location: .?action=list_categories'); // display the Category List page
}
?>
The add_product
function add_product
函数
function add_product($category_id, $first_name, $last_name) {
global $db;
$query = "INSERT INTO products
(categoryID, FirstName, LastName)
VALUES
('$category_id', '$first_name', '$last_name')";
$db->exec($query);
}
?>
Besides @MahfuzulAlam's answer, another problem is: 除了@MahfuzulAlam的答案,另一个问题是:
if(isset($_POST['first_name'])){$first_name = $_POST['FirstName'];}
^^^^^^^^^
if(isset($_POST['last_name'])){$last_name = $_POST['LastName'];}
^^^^^^^^
It should be: 它应该是:
if(isset($_POST['first_name'])){$first_name = $_POST['first_name'];}
if(isset($_POST['last_name'])){$last_name = $_POST['last_name'];}
Also, in PHP 7, there's a shorthand for what you are currently doing: 另外,在PHP 7中,您当前正在执行的操作有一个简写形式:
$first_name = $_POST['first_name'] ?? "";
$last_name = $_POST['last_name'] ?? "";
It's called Null Coalesce Operator . 称为Null Coalesce Operator 。
Try using type = text
not input
. 尝试使用type =
text
not input
。 <input type="text" name="last_name" />
and so on. <input type="text" name="last_name" />
,依此类推。
And there are problems in this code. 并且此代码中存在问题。
if(isset($_POST['first_name'])){$first_name = $_POST['FirstName'];}
if(isset($_POST['last_name'])){$first_name = $_POST['LastName'];}
Whatever you pass as first_name and last_name your $first_name
and $last_name
variables will be empty. 无论您以first_name和last_name传递什么,您的
$first_name
和$last_name
变量都将为空。 Because you are using $_POST['FirstName'], $_POST['LastName']
to set their values respectively, this don't really exists and both returning NULL
. 因为您使用
$_POST['FirstName'], $_POST['LastName']
分别设置它们的值,所以这实际上并不存在,并且都返回NULL
。 Rewrite these line as below: 重写这些行,如下所示:
if(isset($_POST['first_name'])){$first_name = $_POST['first_name'];}
if(isset($_POST['last_name'])){$first_name = $_POST['last_name'];}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.