使用 Post 的 PHP 表单。错误:未定义的数组键。但是当我使用 get 时它会起作用。 (w3schools 示例)

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

我正在尝试在我的网站上制作一个表格,用户可以在其中提交他们的姓名,他们的分数将保存到高分列表中。 (这是一个问答游戏。)

我尝试通过 w3schools 学习使用表单。我使用了这个例子:https://www.w3schools.com/php/php_forms.asp

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

welcome.php 看起来像这样:

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html> 

我逐字复制并粘贴此示例并尝试运行它,但收到此错误消息:

WelcomeWarning: Undefined array key "name" in [filelocation] on line 4

Your email address is:Warning: Undefined array key "email" in [filelocation] on line 5

但是,当我用“get”替换所有“post”时,它就起作用了。为什么?我需要做什么才能使其与 post 一起使用?

编辑:我还在html中留下了“post”,但我用REQUEST替换了welcome.php中的POST。它现在可以工作了,但是我认为它以某种方式使用 GET 而不是 POST,因为我可以看到 URL 中的输入。我绝对需要避免这种情况。 也许这有帮助

谢谢!

javascript php html forms post
7个回答
1
投票

你可以直接运行welcome.php页面。

将您的welcome.php替换为

<html>
<body>
    <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { ?>
    Welcome <?php echo $_POST["name"]; ?><br>
    Your email address is: <?php echo $_POST["email"]; ?>
    <?php }
          else{ ?>
     <script> location.replace("yourHtmlFileName.html") </script>
     <?php     } ?>
</body>
</html> 

1
投票

您链接的 W3Schools 页面有 在同一页面上使用 POST 和 GET 的两个示例。

考虑到您说您没有编辑代码,我认为您很可能复制了使用 GET 的第二个示例的 HTML 代码,同时使用了使用 POST 的第一个示例中的 PHP 代码。代码块是下面是。 坦白说,这种事情有时每个人都会发生,请确保补充足够的水分并继续前进!

我遇到类似问题的另一个问题是,我将 html 文件命名为“index.html”,并将 php 文件命名为“index.php”,因为 php 首先处理 php 文件,在我的情况下,更改名称文件对我有用


0
投票

在你的索引页面中尝试这个 php 代码:


0
投票

正如您所说,如果您从 w3schools 复制并粘贴了一个示例并得到了这些错误,
检查您是否使用带有实时服务器插件的 VSCode

0
投票
错误:未定义的数组键

的起源。 <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>QUALITY</title> <link rel="stylesheet" href="../css/style.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Noto+Serif:wght@800&family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet"> </head> <body> <?php include './connect.php'; ?> <?php $fullName = $_POST['contact_name']; $email = $_POST['contact_email']; $companyName = $_POST['contact_company']; $login_form = $conn->prepare("insert into contact_info(contact_name, contact_email, contact_company) values(?, ?, ?)"); $login_form->bind_param("sss", $fullName, $email, $companyName); $execval = $login_form->execute(); $login_form->close(); ?> <header> <div class="container-fluid"> <div class="border-bottom pb-3 pt-2"> <img class="header-image" src="../images/Maxbyte.jpg" alt="Maxbyte"> </div> </div> </header> <section class="h-100vh bg-light"> <div class="container p-4"> <div class="d-flex justify-content-between align-items-center mb-3"> <h4 class="mb-4 text-center fw-bold">QUALITY</h4> <select name="" id="to_value" class="p-2 rounded bg-light border-2 border-2 w-150px" required> <option value="USD">USD</option> <option value="INR">INR</option> <option value="AED">AED</option> </select> </div> <div class="border rounded p-4 mx-auto bg-white"> <form id="myForm" class="row g-4 mb-0"> <div class="col-lg-6 col-md-6 pb-1"> <label class="mb-2">Hourly cost for Quality person</label> <div class="h-33px d-flex justify-content-center flex-column"> <input type="text" id="hourlyCost" class="border rounded p-2 px-3 bg-light w-100" name="hourlyCost" data-currentcurrency="USD" required> </div> </div> <div class="col-lg-6 col-md-6 pb-1"> <label class="mb-2">No of Machines</label> <div class="h-33px d-flex justify-content-center flex-column"> <input type="text" id="machines" class="border rounded p-2 px-3 bg-light w-100" name="machines" required> </div> </div> <div class="col-lg-6 col-md-6 pb-1"> <label class="mb-2">Machine running cost per hour</label> <div class="h-33px d-flex justify-content-center flex-column"> <input type="text" id="machines_hourly_running_cost" class="border rounded p-2 px-3 bg-light w-100" name="machines_hourly_running_cost" required> </div> </div> <div class="col-lg-6 col-md-6 pb-1"> <label class="mb-2">Documentation cost machine per year</label> <div class="h-33px d-flex justify-content-center flex-column"> <input type="text" id="cost_machine" class="border rounded p-2 px-3 bg-light w-100" name="cost_machine" data-currentcurrency="USD" required> </div> </div> <div class="col-lg-6 col-md-6 pb-1"> <label class="mb-2">Man Hours per day to copy data from paper forms into database</label> <div class="h-33px d-flex justify-content-center flex-column"> <input type="text" id="hours_per_day" class="border rounded p-2 px-3 bg-light w-100" name="hours_per_day" required> </div> </div> <div class="col-lg-6 col-md-6 pb-1"> <label class="mb-2">Number of New setups per month</label> <div class="h-33px d-flex justify-content-center flex-column"> <input type="text" id="setups" class="border rounded p-2 px-3 bg-light w-100" name="setups" data-currentcurrency="USD" required> </div> </div> <!-- <div><input type="hidden" class="border rounded p-2 px-3 bg-light w-10" id="to_email" name="to_email"></div> --> <div class="col-12 text-center"> <button type="submit" class="px-5 py-2 rounded text-white border-0 text-decoration-none">Calculate</button> </div> </form> </div> </div> </section> <script> // var email = "<?php echo "$email" ?>" // document.getElementById("to_email").value = email // document.addEventListener("DOMContentLoaded", function () { // convertCurrency(); // }); const apiKey = "Xi0wAt0uz97w1Am9aSLS6GUkfSKZG7tl"; function change_hourly_cost() { const fromCurrency = document.getElementById("hourlyCost").dataset.currentcurrency; // alert(fromCurrency); const toCurrency = document.getElementById("to_value").value; // alert(toCurrency); const amount = document.getElementById("hourlyCost").value; const url = `https://api.apilayer.com/currency_data/convert?apikey=${apiKey}&from=${fromCurrency}&to=${toCurrency}&amount=${amount}`; fetch(url) .then(response => response.json()) .then(data => { const convertedAmount = data.result; document.getElementById("hourlyCost").value = convertedAmount; // Display the profit_unit in the div // document.getElementById("profit_unit").textContent = `${convertedAmount} ${toCurrency}`; document.getElementById("hourlyCost").setAttribute("data-currentcurrency", toCurrency); }) .catch(error => console.error(error)); } document.getElementById("to_value").addEventListener("input", change_hourlyCost); function change_machines_hourly_running_cost() { const fromCurrency = document.getElementById("machines_hourly_running_cost").dataset.currentcurrency; const toCurrency = document.getElementById("to_value").value; const amount = document.getElementById("machines_hourly_running_cost").value; const url = `https://api.apilayer.com/currency_data/convert?apikey=${apiKey}&from=${fromCurrency}&to=${toCurrency}&amount=${amount}`; fetch(url) .then(response => response.json()) .then(data => { const convertedAmount = data.result; document.getElementById("machines_hourly_running_cost").value = convertedAmount; // Display the profit_unit in the div // document.getElementById("profit_unit").textContent = `${convertedAmount} ${toCurrency}`; document.getElementById("machines_hourly_running_cost").setAttribute("data-currentcurrency", toCurrency); }) .catch(error => console.error(error)); } document.getElementById("to_value").addEventListener("input", change_machines_hourly_running_cost); function change_cost_machine() { const fromCurrency = document.getElementById("cost_machine").dataset.currentcurrency; const toCurrency = document.getElementById("to_value").value; const amount = document.getElementById("cost_machine").value; const url = `https://api.apilayer.com/currency_data/convert?apikey=${apiKey}&from=${fromCurrency}&to=${toCurrency}&amount=${amount}`; fetch(url) .then(response => response.json()) .then(data => { const convertedAmount = data.result; document.getElementById("cost_machine").value = convertedAmount; // Display the profit_unit in the div // document.getElementById("profit_unit").textContent = `${convertedAmount} ${toCurrency}`; document.getElementById("cost_machine").setAttribute("data-currentcurrency", toCurrency); }) .catch(error => console.error(error)); } document.getElementById("to_value").addEventListener("input", change_cost_machine); document.getElementById('myForm').addEventListener('submit', function (e) { e.preventDefault(); // Capture the input values const hourlyCost = parseFloat(document.getElementById('hourlyCost').value); const machines = parseFloat(document.getElementById('machines').value); const machines_hourly_running_cost = parseFloat(document.getElementById('machines_hourly_running_cost').value); const cost_machine = parseFloat(document.getElementById('cost_machine').value); const hours_per_day = parseFloat(document.getElementById('hours_per_day').value); const setups = parseFloat(document.getElementById('setups').value); const selectedCurrency = document.getElementById('to_value').value; // Calculate the result const CostPerMachine = cost_machine * machines; const costSavedResponse = (((15 / 60) * hours_per_day * runningCost * hourlyCost) * 5) * 12; const costSavedManPower = hours_per_day * hourlyCost; const costSaved = CostPerMachine + costSavedResponse + costSavedManPower; let implementation; if (selectedCurrency === 'USD') { implementation = machines * 500 + machines * 100; } else if (selectedCurrency === 'INR') { implementation = machines * 83282.50 + machines * 8328.25; } else if (selectedCurrency === 'AED') { implementation = machines * 3673.00 + machines * 367.30; } const ROI = (implementation / costSaved) * 12; // const emailto = "<?php echo "$email" ?>" // console.log("email", emailto); // Store the result in localStorage localStorage.setItem('CostPerMachine', CostPerMachine); localStorage.setItem('costSavedResponse', costSavedResponse); localStorage.setItem('costSavedManPower', costSavedManPower); localStorage.setItem('costSaved', costSaved); localStorage.setItem('selectedCurrency', selectedCurrency); localStorage.setItem('implementation', implementation); localStorage.setItem('ROI', ROI); localStorage.setItem('hourlyCost', hourlyCost); localStorage.setItem('machines', machines); localStorage.setItem('machines_hourly_running_cost', machines_hourly_running_cost); localStorage.setItem('cost_machine', cost_machine); localStorage.setItem('hours_per_day', hours_per_day); localStorage.setItem('setups', setups); // Redirect to the second page window.location.href = `./form-2.php?costSaved=${costSaved}&ROI=${ROI}&hourlyCost=${hourlyCost}&machines=${machines}&machines_hourly_running_cost=${machines_hourly_running_cost}&cost_machine=${cost_machine}&hours_per_day=${hours_per_day}&setups=${setups}`; }); </script> <script src="../js/script.js"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> </body> </html>


0
投票
我收到此消息是因为有条件(PHP)生成的 html 表单并不总是显示。如果显示,一切顺利。如果没有,则数组丢失。因此,与其不将输入字段包含在输出中,不如隐藏输入字段。

0
投票

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