I am just a beginner with PHP & Html. I have seen many questions on SO related to this, but somehow cannot fix a very simple looking problem at my end. So, kindly help.
The following code does not seem to be working. There is always a message "There seems to be a problem right now. Please try again after sometime" before the form when I run this code. Whether I press the submit button or not, it does not make any difference.
<h2 >Inquiry form</h2>
<?php
if (isset($_POST['submit'])){
echo "Thank You!";
}
else {
echo "There seems to be a problem right now. Please try again after sometime";
}
?>
<form name="input" method="POST" action="contact.php">
<label for="Name">Name (required):</label>
<br />
<input type="text" name="Name" />
<br />
<div class="clear"></div>
<label for="inputmail">Email(required):</label>
<br />
<input type="text" name="email" />
<br />
<div class="clear"></div>
<label for="inputtelefon">Phone:</label>
<br />
<input type="text" name="phone" />
<br />
<div class="clear"></div>
<label for="inputmessage">Message:</label>
<br/>
<textarea name="message" cols="28" rows="3" ></textarea>
<div class="clear"></div>
<div id="send">
<input type="submit" value=" Submit " />
<input type="reset" value=" Clear " />
</div>
</form>
Changed code:
<h2 >Inquiry form</h2>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//if ($_SERVER['REQUEST_METHOD'] == 'POST') { This is not working as well
echo "Thank You!";
}
else {
echo "There seem to be a problem right now. Please try again after sometime";
}
?>
<form name="input" method="POST" action="contact.php">
<label for="Name">Name (required):</label>
<br />
<input type="text" name="Name" />
<br />
<div class="clear"></div>
<label for="inputmail">Email(required):</label>
<br />
<input type="text" name="email" />
<br />
<div class="clear"></div>
<label for="inputtelefon">Phone:</label>
<br />
<input type="text" name="phone" />
<br />
<div class="clear"></div>
<label for="inputmessage">Message:</label>
<br/>
<textarea name="message" cols="28" rows="3" ></textarea>
<div class="clear"></div>
</div>
<div id="send">
<input type="submit" value=" Submit " name="submit"/>
</div>
</form>
That's because your submit button does not have a name:
<input type="submit" value=" Submit " name="submit"/>
Edit
Based on the comment of RandomCoder (in Marc B's answer).
Found this similar question: isset($_POST['submit']) vs $_SERVER['REQUEST_METHOD']=='POST'
Invalid method of checking for a submission. Never check for the presence/absence of a form field. It's unreliable. You might change the field name, but forget to update the PHP. Use this instead:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
...
}
it's 100% reliable and will always be "true" if a POST was performed.
Take a look at your updated if statement. It is saying: if the form has been submitted then show "thank you", if the form hasn't been submitted then show "there seems to be a problem".
That's the reason you're always seeing "there seems to be a problem", because when you view the form without submitting it, the else block of your if statement fires.
You should remove the else block because it doesn't make any sense, so it becomes:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// form has been submitted, handle the data and say thanks...
echo "Thank You!";
}
Using this method instead of checking for the submit button is better because Internet Explorer will not send the submit button as a post variable if the user pressed the enter key to submit the form.
More info - Why isset($_POST['submit']) is bad.
There is Only one reasons which I see i.e. you have not defined the name submit to any variable. There are two ways to find the submitting form as:
Either change:
<input type="submit" value=" Submit " />
to
<input type="submit" value=" Submit " name="submit"/>
or add the extra line to do things something like this
<input type = "hidden" name="passed" />
and to check
<?php
if (isset($_POST['passed'])){
echo "Thank You!";
}else {
echo "There seems to be a problem right now. Please try again after sometime";
}
?>
Related
I am building a WordPress plugin for my livechat. When someone downloads the plugin, I want them to fill out some information (name, e-mail, etc). After submitting that info, the form has to disappear/hide. For some reason I am not successful and imo I've tried everything. At the moment I'm trying to do it with an if-statement checking if the submit-button isset(). Unfortunately that didn't work.
Can someone please help me? The code for display the form and the page after submitting:
<?php
public function display_plugin_setup_page()
{
if (isset($_POST['submitForm'])) {
?>
<form action="options.php" method="post">
<?php
settings_fields('mister_chat_options');
do_settings_sections($this->plugin_name); ?>
<input name="submit" class="button button-primary" type="submit" value="<?php esc_attr_e('Save'); ?>" />
</form>
<?php
} else {
// create the form
?>
<form method="post" action="sendmail.php">
<input type="hidden" name="formSent">
<fieldset>
<input placeholder="Voornaam" type="text" id="vnaam" name="vnaam">
</fieldset>
<fieldset>
<input placeholder="Achternaam" type="text" id="anaam" name="anaam">
</fieldset>
<fieldset>
<input placeholder="Bedrijfsnaam" type="text" id="bnaam" name="bnaam">
</fieldset>
<fieldset>
<input placeholder="E-mailadres" type="email" id="email" name="email">
</fieldset>
<fieldset>
<input placeholder="Telefoonnummer" type="tel" id="telef" name="telef">
</fieldset>
<fieldset>
<input type="submit" name="submitForm" id="contact-submit" data-submit="...Verzenden">
</fieldset>
</form>
<?php
}
}
I placed the sendmail.php file inside the file above and that fixed my problem.
I don't know what the heck the problem is with this form not working. The form is not to be sent anywhere. It's just a questionnaire for clients to fill/print out if they like, but I cannot get the results to post into the hidden div. What am I doing wrong? I installed a wordpress php plugin and other php echo's work, but not the POST thing. The HTML:
<form method="post">
<input type="text" placeholder="Your Name" name="yourname" />
<br /><br />
<input type="text" placeholder="Pet's Name" name="petsname" />
<br /><br />
<input type="text" placeholder="Pet's Birth Date" onfocus="(this.type='date')" onblur="(this.type='text')" id="date" name="petsbirthdate" />
<br /><br />
<strong>Pet's Sex</strong>: <input type="radio" value="Male" name="sex" id="Male" /><label for="Male">Male</label> <input type="radio" value="Female" name="sex" id="Female" /><label for="Female">Female</label>
<button type="button" class="button" onclick="show_output();">Display
Answers</button>
</form>
Pay no attention to the onclick handler on the button. I'm not including that JavaScript, but just know that the hidden div does show when the button is clicked. The PHP that returns no form results: Pay no attention to the PHP in brackets, that's the way it works in WordPress with the plugin.
<p class="data_answers">
<strong>Your Name:</strong> [php]echo htmlspecialchars($_POST['yourname']);[/php]</p>
<p class="data_answers">
<strong>Pet's Name:</strong> [php]echo $_POST['petsname'];[/php]</p>
<p class="data_answers">
<strong>Pet's Birth Date:</strong> [php]echo $_POST['petsbirthdate'];[/php]</p>
<p class="data_answers">
<strong>Pet's Sex:</strong> [php]echo $_POST['sex'];[/php]</p>
The PHP that will work (anywhere; I even placed it in the middle of, where the form answers should be, and it shows up. So why doesn't the other work?
[php] echo "Hello world!"; [/php]
Thank You!
Tracy
P.S. Although you cannot see the PHP, but the actual page in draft is here: http://www.safarivet.com/behavior-conditions/
You should add the action attribute in the form tag
<form method="post" action="">
..leads back to the same page
Extending from Johannes answer, you can also use a number sign # or the global variable $_SERVER['PHP_SELF']
Either of these will work
<form method="post" action"">
<form method="post" action"#">
<form method="post" action"<?php echo $_SERVER['PHP_SELF']?>">
haven't programmed PHP in a while but I
have to assemble something for a client really fast.
I've set up 2 forms with POST but when I go to the next file it's just blank space, for some reason POST isn't being registered but is set cause I'm not getting an error echo.
Hese's the forms:
<form action="Funkcije.php" method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj">
</form>
<br>
<div id="newItem">
<form action="Funkcije.php" method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj">
</form>
</div>
And here's the 2nd file:
if(isset($_POST["AddFromDB"], $_POST["ArtNo"])){
addExisting ($_POST["ArtNo"]);
}
else if(isset($_POST["AddNew"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
newItem ($_POST["Art"] && $_POST["ImeProizvoda"] && $_POST["Dobava"] && $_POST["Cijena"]);
}
else if (!isset ($_POST)){
echo "error";
}
So, by code I should be getting an error if POST is not set but I get nothing. Just a blank space.
here, you must be give a name to the submit button to check which form is POST like this...
<form method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj" name="form1">
</form>
<br>
<div id="newItem">
<form method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj" name="form2">
</form>
</div>
<?php
if(isset($_POST["form1"], $_POST["ArtNo"])){
echo "1";
}
else if(isset($_POST["form2"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
echo "2";
}
else{
echo "error";
}
?>
now, this work fine..
thank you.. enjoy coding...
I have an html form where people can subscribe to a mailing list. The form includes form validation and when the form is submitted, the data is stored in a database using My SQL.
Here is the code on the index.html page where the form is
<form id="subscribe-form" action="send.php" method="post">
<p id="status"></p>
<div>
<label for="title">Title:</label>
<select class="uniform" name="title" id="title">
<option>Please Choose</option>
<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Ms</option>
</select>
</div>
<div>
<label for="firstName">First name:</label>
<input type="text" id="firstName" name="firstName" />
</div>
<div>
<label for="surname">Surname:</label>
<input type="text" id="surname" name="surname" />
</div>
<div>
<label for="email">Email:</label>
<input type="text" id="email" name="email" />
</div>
<div>
<label for="phone">Contact Number:</label>
<input type="text" id="phone" name="phone" />
</div>
<div>
<label for="title">How did you hear about us?</label>
<select class="uniform" name="refer" id="refer">
<option>Please Choose</option>
<option>Google</option>
<option>Yahoo</option>
<option>Word of Mouth</option>
<option>Others</option>
</select>
</div>
<div>
<input type="checkbox" name="news_updates" value="1" />
I'd like to hear about the latest news and events updates</div>
<div>
<input class="button" type="submit" value=""/>
</div>
</form>
Here is the code for send.php
<?php
include ('connection.php');
$sql="INSERT INTO form_data (title,firstName, surname, email, phone, refer, news_updates)
VALUES
('$_POST[title]', '$_POST[firstName]','$_POST[surname]','$_POST[email]','$_POST[phone]','$_POST[refer]','$_POST[news_updates]')";
if (!mysql_query($sql, $connected))
{
die('Error: ' . mysql_error());
}
mysql_close($connected);
?>
I would like to make another html (unsubscribe.html) page where people can unsubscribe by entering their email address so that their email address would match the corresponding email that is in the database already and remove it from the My Sql database .
I found this tutorial which was kind of helpful -
http://www.phpsuperblog.com/php/delete-records-from-mysql-database-with-html-form-and-php/
and this is the form on my unsubscribe.html page.
<form id="unsubscribe_form" action="delete.php" method="post">
<div>
<label for="email_remove">Email:</label>
<input type="text" id="email_remove" name="email_remove" />
</div>
<div>
<input name="delete" type="submit" id="delete" value="" class="unsubscribe_btn">
</div>
</form>
but when I enter method="post" in the unsubscribe form. The data from the form on the subscribe / index.html does not get stored in My Sql, instead they come up as blank.
So I am guessing I can't have two "post" method maybe??
If someone could guide me in the right direction that would be much appreciate. Thanks.
I guess you are at your learning stage. So, I will suggest you to have a check for POST method being called on the page which receives the post.
Example: in your subscribe.php
you should have :
<input class = "button" type = "submit" value = "Subscribe" name = "subscribe" />
in send.php
you must do:
if(!isset($_POST['subscribe'])
{
header('location: subscribe.html');
}
You must use isset for your pages.
If you could display your delete.php, perhaps I can edit this post and assist you further but, so far... A check is required and you can use as many forms as many you like (even on one page) but, make sure they all have different id/names.
Your delete.php script should be:
<?php
require ('connection.php'); // User require for important functions so that if not found, it throws fatal error
$email = $_POST['email_remove'];
// Check for isset POST
$query = "DELETE from form_data WHERE email = '".$email."'";
if(mysql_query($query)){ echo "deleted";} else{ echo "fail";}
?>
your delete.php seems OK to me.
Can add the following to Line 2
echo "";
print_r($_POST);
and post array in comments?
I'm trying to create a BMI calculator. This should allow people to use either metric or imperial measurements.
I realise that I could use hidden tags to solve my problem, but this has bugged me before so I thought I'd ask: I can use $_POST['variableName'] to find the submitted variableName field-value; but...I don't know, or see, how to verify which form was used to submit the variables.
My code's below (though I'm not sure it's strictly relevant to the question):
<?php
$bmiSubmitted = $_POST['bmiSubmitted'];
if (isset($bmiSubmitted)) {
$height = $_POST['height'];
$weight = $_POST['weight'];
$bmi = floor($weight/($height*$height));
?>
<ul id="bmi">
<li>Weight (in kilograms) is: <span><?php echo "$weight"; ?></span></li>
<li>Height (in metres) is: <span><?php echo "$height"; ?></span></li>
<li>Body mass index (BMI) is: <span><?php echo "$bmi"; ?></span></li>
</ul>
<?php
}
else {
?>
<div id="formSelector">
<ul>
<li>Metric</li>
<li>Imperial</li>
</ul>
<form name="met" id="metric" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Kilograms">kg</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (<abbr title="metres">m</abbr>):</label>
<input type="text" name="height" id="height" />
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<form name="imp" id="imperial" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Pounds">lbs</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (Inches):</label>
<input type="text" name="height" id="height" /
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<?php
}
?>
I verified that it worked (though without validation at the moment -I didn't want to crowd my question too much) with metric; I've added the form but not the processing for the imperial yet.
To identify the submitted form, you can use:
A hidden input field.
The name or value of the submit button.
The name of the form is not sent to the server as part of the POST data.
You can use code as follows:
<form name="myform" method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="frmname" value=""/>
</form>
You can do it like this:
<input type="text" name="myform[login]">
<input type="password" name="myform[password]">
Check the posted values
if (isset($_POST['myform'])) {
$values = $_POST['myform'];
// $login = $values['login'];
// ...
}
The form name is not submitted. You should just add a hidden field to each form and call it a day.
In the form submitting button (id method of form is post):
<input type="submit" value="save" name="commentData">
In the PHP file:
if (isset($_POST['commentData'])){
// Code
}
For some reason, the name of the submit button is not passed to the superglobal $_POST when submitted with Ajax/jQuery.
Use a unique value on the submit button for each form like so
File index.html
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="contact">Send Message</button>
</form>
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="support">Send Message</button>
</form>
File email.php
<?php
if (isset($_POST["submit"])) {
switch ($_POST["submit"]) {
case "contact":
break;
case "support":
break;
default:
break;
}
}
?>
As petervandijck.com pointed out, this code may be susceptible to XSS attacks if you have it behind some kind of log-in system or have it embedded in other code.
To prevent an XSS attack, where you have written:
<?php echo "$weight"; ?>
You should write instead:
<?php echo htmlentities($weight); ?>
Which could even be better written as:
<?=htmlentities($weight); ?>
You can use GET in the form's action parameter, which I use whenever I make a login/register combined page.
For example: action="loginregister.php?whichform=loginform"
I had a similar problem which brought me to this question. I reviewed all the preceding answers, but ultimately I ending up figuring out my own solution:
<form name="ctc_form" id="ctc_form" action='' method='get'>
<input type="hidden" name="form_nm" id="form_nm">
<button type="submit" name="submit" id="submit" onclick="document.getElementById('form_nm').value=this.closest('form').name;">Submit</button>
</form>
It seamlessly and efficiently accomplishes the following:
Passes the form name attribute via a hidden input field, without using the fallible value attribute of the submit button.
Works with both GET and POST methods.
Requires no additional, independent JavaScript.
You could just give a name to the submit button and do what needs to be done based on that. I have several forms on a page and do just that. Pass the button name and then if button name = button name do something.
Only the names of the form fields are submitted, but the name of the form itself is not. But you can set a hidden field with the name in it.