简体   繁体   English

在一页而不是两页中包含PHP代码

[英]Having PHP code in one page rather than two

I have a number of registration pages on my web application and a number of textboxes in a form that pushes the values enter to another php page which writes it to the database. 我的Web应用程序上有许多注册页面,还有一些文本框,它们以某种形式将值输入到另一个将其写入数据库的php页面中。 The code for it looks like this 它的代码如下所示

 <form action = "submitEvent.php" method = "post"> Event Name: <br> <input type = "text" name = "eventname" > <br> Event Type: <br> <input type = "text" name = "eventtype" > <br> Charity Number: <br> <input type = "text" name = "charityid" > <br> Contact Details: <br> <input type = "text" name = "contactdetails" > <br> Location: <br> <input type = "text" name = "eventlocation" > <br> Date : <br> <input type ="date" name ="eventdate"> <br> <input type = "submit" value = "Submit"> </form> 

and then the submitEvent.php file looks like this 然后,submitEvent.php文件如下所示

 <?php $eventname = filter_input(INPUT_POST, 'eventname'); $eventtype = filter_input(INPUT_POST, 'eventtype'); $charitynumber = filter_input(INPUT_POST, 'charityid'); $contactdetails = filter_input(INPUT_POST, 'contactdetails'); $eventlocation = filter_input(INPUT_POST, 'eventlocation'); $eventdate = filter_input(INPUT_POST, 'eventdate'); $servername = "localhost"; $username = "root"; $password = ""; $dbname = "fyp"; //Create new connection $conn = new mysqli($servername, $username, $password, $dbname); //Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; $sql = "INSERT INTO event (eventname, eventtype, charityid, contactdetails, location, date) VALUES ('$eventname', '$eventtype', '$charitynumber', '$contactdetails', '$eventlocation', '$eventdate')"; if ($conn->query($sql) === TRUE) { echo "New record created succesfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; ?> 

Is there an easier way to do this as I have a number of pages like this? 我有很多这样的页面,有没有更简单的方法可以做到这一点? Would I be able to put it into one page? 我可以将其放入一页吗?

Yes, it is possible to have all this in one page. 是的,所有这些都可以放在一页中。 You can use isset($_POST) to check if there is data available and then process it. 您可以使用isset($ _ POST)来检查是否有可用数据,然后进行处理。 But modulation (having separate files) makes it easy. 但是调制(具有单独的文件)使操作变得容易。 It helps to debug when you have php and html codes in diff files. 当您在diff文件中包含php和html代码时,它有助于调试。

You can check if the form is submitted or not and act accordingly. 您可以检查该表单是否已提交并采取相应措施。

To check if you the form is submitted you can check if $_POST['Submit'] is set. 要检查您是否提交了表单,可以检查是否设置了$_POST['Submit']

<?php 

if(isset($_POST['Submit'])){
  /* PHP code here */
}

?>
<!-- And point the form to submit to itself. -->

<form action="thisPage.php" method="post"> 
<!-- Rest of form here -->

The most basic way is add a hidden field to your form and on the form page check for that in php. 最基本的方法是在表单中添加一个隐藏字段,然后在表单页面上检查php中的内容。

HTML 的HTML

<form action = "form_page.php" method = "post">
<input type="hidden" name="submitted" value="1">
.....
</form>

PHP: PHP:

<?php

if(isset($_POST["submitted"])){

/*PHP processing code goes here*/

}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM