I want to have a php contact form, that when submitted (if no errors), will refresh the same page, and remove the contact form, and replace it with some text e.g. "thank you for contacting us".
Is there a best way to do this?
You could always do something like this:
<?php
$posted = false;
if( isset($_POST['submit']) ) {
/* Process $_POST */
/* Do your things */
/* Set a variable hinting if a post has been done */
$posted = true;
}
?>
<!DOCTYPE html>
<html>
<body>
<?php if( $posted ): ?>
<form method="post">
<input name="foo" />
<input name="bar" />
<input name="car" />
<input name="submit" type="submit" value="Submit" />
</form>
<?php else: ?>
<h1>Thank you for contacting us!</h1>
<?php endif; ?>
</body>
</html>
Do it this way.
<?php
$showform=true; //dispaying the form for the first time
if(isset($_POST['submit'])){
//check for errors
if($errors){
$msg="errors";
$showform=true; // if found errors, setting error msg and displaying the form
}else{
//process the form
$showform=false; //if no errors, setting success msg and hiding the form
$msg="SUccess";
}
}
if(isset($msg)){
//display the success/error msg
echo $msg;
}
if($showform==true){
//your form code
<form method="post">
<input name="foo" />
<input name="bar" />
<input name="car" />
<input name="submit" type="submit" value="Submit" />
</form>
}
?>
Related
I updated the question.
Since the last code was pretty complex and even after fixing the stuff it didn't work, I executed the below simple code to check if things work. Even this code doesn't work. Whenever I click on the submit button, it again returns a 404 error.
Yes, I placed the PHP code in the body as well to check if this work but it doesn't.
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<html>
<head>
<title>Echo results!</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
</body>
</html>
Try giving the button_create as name of the submit button
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
if(isset($_POST['button_create'])) {
<td><input type="submit" name="button_create" id="button_create" value="Create Table!"></td>
change these lines see how you go from there
There are a couple of things wrong here, method should be POST instead of GET. The name attribute of text fields should be used when receiving the values. The submit button name should be used to check whether the button is clicked or not. See the example given below.
<?php
if (isset($_POST['submit'])) {
$ex1 = $_POST['ex1'];
$ex2 = $_POST['ex2'];
echo $ex1 . " " . $ex2;
}
?>
<form action="" method="post">
Ex1 value: <input name="ex1" type="text" />
Ex2 value: <input name="ex2" type="text" />
<input name="submit" type="submit" />
</form>
Echo results!
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
this is for your updated question
I am playing around with using my Raspberry Pi 3 as a web server.
I would like to learn more about processing user input through forms.
I have two files in /var/www/html, viz. form.html and form.php:
form.html:
<form action="form.php" method="post">
<input type="text" name="varname"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
<?php
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['varname'] = $_POST['varname'];
}
?>
form.php:
<?php
session_start();
$var_value = htmlspecialchars($_SESSION['varname']);
echo $var_value;
?>
When I click Submit! on form.html the browser takes me to form.php which displays a blank page.
Naturally, I would like it to print $var_value to the screen.
Is there problem in my code, or could it be some other server-side issue?
Change your form.html extension to form.php,
And you may use the below code to achieve your work.
Form.php // Single page
<?php
session_start(); // Should be in first Line
if (isset($_POST['Submit'])) {
$_SESSION['varname'] = $_POST['varname'];
$var_value = htmlspecialchars($_SESSION['varname']);
echo $var_value;
}
?>
<form method="post">
<input type="text" name="varname"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
?>
Otherwise: // Multiple Page
form.php
<form action="some_form.php" method="post">
<input type="text" name="varname"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
some_form.php
<?php
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['varname'] = $_POST['varname'];
$var_value = htmlspecialchars($_SESSION['varname']);
echo $var_value;
}
?>
I have this code :
<html>
<head>
<title>Title</title>
</head>
<body>
<form name="selectForm" id="selectForm" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type="checkbox" name="checkbox" form="selectForm" />CheckBox
</form>
<input type="button" value="Submit" name="submit" onclick="document.selectForm.submit();" />
<?php
if(isset($_POST['submit'])) {
echo "Submitted";
} else {
echo "Not submitted";
}
?>
</body>
</html>
I can not detect if the form was submitted. I would like to modify the PHP code and not the html, if possible.
Previously I had my Submit type input inside the form and it worked, but now that it is outside and I use JavaScript to do the submit it does not work.
How can I detect if the form is submitted ?
The $_POST['submit'] will no longer exist, since the submit button is no longer part of the form. Instead we can check the $_SERVER['REQUEST_METHOD'].
if($_SERVER['REQUEST_METHOD'] === 'POST') {
echo 'Submitted';
}
Also, if you leave the form's action attribute blank, it will submit to the current page:
<form name="selectForm" id="selectForm" action="" method="post">
Update:
Add a hidden field with the name of your form:
<input type="hidden" name="formname" value="selectForm" />
<?php
if(isset($_POST['formname'])) {
echo $_POST['formname'] . ' submitted';
}
?>
I'm doing php that is textbox a value empty it will open a alertbox (I'm using javascript in here )
this is my code
<?php
include('config.php');
if(isset($_POST['submit'])){
$username=$_POST['username'];
?>
<script>
function validate(){
if(document.forms[0].username.value==""){
window.alert("You must enter both values");
return false;
}
}
</script>
<?php
}
?>
<html>
<div><p>Member Profile</p>
<form action="testing.php" method="POST" onsubmit="return validate();">
Username<br>
<input class="user" type="text" name="username" id="username" /><br>
<input type="submit" name="submit" value="register" />
</form>
</div>
</html>
The problem is i have to click 2 times before the alert show
please help me to solve this problem
It's because the script is inside the php if(isset){} block, one click submits the form, which generates the script and then it works the second time.. try this setup instead:
<?php
include ('config.php');
if (isset($_POST['submit']))
{
$username = $_POST['username'];
}
?>
<html>
<head>
<script>
function validate () {
if (document.forms[0].username.value == "") {
window.alert("You must enter both values");
return false;
}
}
</script>
</head>
<body>
<div>
<p>
Member Profile
</p>
<form action="testing.php" method="POST" onsubmit="return validate();">
Username
<br>
<input class="user" type="text" name="username" id="username" />
<br>
<input type="submit" name="submit" value="register" />
</form>
</div>
</body>
</html>
Edit:
I've moved the script tag inside the head tag. I'm not sure if there are any implications for having the script outside but just to be sure I've moved it.
2nd Edit: (OCD is kicking in)
I've added body tags, not sure if you copied and pasted this code but it looked weird to me :)
On basis of the information that the user fills in my form, I want to execute some PHP code. So after the form is submitted, it should have control on the same page and thereafter it should execute the PHP code (and not before pressing submit button). I have used <input type="hidden" value=1 name="hid"/>. When the user clicks the submit button, the value is changed to 0. But its not working. so solution please..
Is this similar to what you are looking for ?
<?php
if (!isset($_POST["submit"]) ) {
if ($_POST["hid"] == 0 ) {
echo "hid is not 0. display form.";
}
?>
<html>
<head>
<script type="text/javascript">
function check_valid() {
document.getElementById("hid").value = 0;
}
</script>
</head>
<body>
<form method="POST" action="<?php echo $PHP_SELF;?>" onsubmit="return check_valid();" >
<input type="hidden" id="hid" name="hid" value="1" />
<input type="submit" value="submit" name="submit"/>
<!-- form elements go here -->
</form>
</body>
</html>
<?php
} else {
echo "hid is now 0, execute the php code";
}
?>
EDIT: added <input type="hidden" name="hid" value="1" /> for clarity. Thanks to andre_roesti for the suggestion