提交表单到行动的PHP文件 [英] Submit form to action php file
问题描述
我有一个表单,当用户点击提交时,我需要运行一个php文件。
< form action =php_scripts / test.phpmethod =POST >
< input name =featuretype =textplaceholder =Feature/>
< input name =feature2type =textplaceholder =Feature2/>
< input type =submitvalue =submit/>
< / form>
test.php
<?php
if(isset($ _ GET ['submit']))
{
$ feature = $ _POST ['feature'] ;
// do stuff(将数据发送到数据库)
}
?>
我遇到的问题是,当我在表单上按提交时,
if(isset($ _ GET ['submit']))
总是返回false。
任何人都可以解释这是为什么吗?我完全误解了如何实现向PHP脚本发送数据的形式?
如果我犯了任何语法错误,并且非常感谢您提供任何帮助,请致歉。
您的代码有一些错误。
将GET与POST方法混合使用。另外,为您的输入添加值并且您的提交按钮未命名,您试图将其用作条件语句。$ b $ p
< form action =php_scripts / test.phpmethod =POST>
< input name =featurevalue =featuretype =textplaceholder =Feature/>
< input name =feature2value =feature2type =textplaceholder =Feature2/>
< input type =submitname =submitvalue =submit/>
< / form>
PHP
if(isset($ _ POST ['submit']))
{
$ feature = $ _POST ['feature'];
$ feature2 = $ _POST ['feature2'];
// do stuff(将数据发送到数据库)
}
?>
旁注:您可以/也应该检查空值。
if(isset($ _ POST ['submit'])
&&!empty($ _ POST ['feature'])
&&!empty($ _ POST ['feature2'])){...}
脚注:
看到您打算发送给DB:
我希望你计划使用 mysqli
with prepared statements 或 包含准备好的语句的PDO 。
I have a form where when the user clicks submit, I need a php file to be ran. below is the form and the php file.
<form action="php_scripts/test.php" method="POST">
<input name="feature" type = "text" placeholder="Feature" />
<input name="feature2" type = "text" placeholder="Feature2" />
<input type="submit" value = "submit"/>
</form>
test.php
<?php
if( isset($_GET['submit']) )
{
$feature = $_POST['feature'];
// do stuff (will send data to database)
}
?>
The problem I am having is that when I press Submit on the form,
if( isset($_GET['submit']) )
Always returns false.
Can anyone explain why that is? Have I totally misunderstood how to implement form sending data to php scripts?
Apologies if I have made any syntax errors and many thanks for any help you can give.
There are a few things wrong with your code.
You're mixing GET with POST methods. Plus, add values to your inputs and your submit button isn't named, which you're trying to use as a conditional statement for.
HTML
<form action="php_scripts/test.php" method="POST">
<input name="feature" value="feature" type = "text" placeholder="Feature" />
<input name="feature2" value="feature2" type = "text" placeholder="Feature2" />
<input type="submit" name="submit" value = "submit"/>
</form>
PHP
<?php
if( isset($_POST['submit']) )
{
$feature = $_POST['feature'];
$feature2 = $_POST['feature2'];
// do stuff (will send data to database)
}
?>
Sidenote: You could/should also check against empty values.
if(isset($_POST['submit'])
&& !empty($_POST['feature'])
&& !empty($_POST['feature2']) ) {...}
Footnotes:
Seeing that you're intending on sending to DB:
I hope you plan on using mysqli
with prepared statements, or PDO with prepared statements.
这篇关于提交表单到行动的PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!