So I'm trying to create a basic form using HTML and PHP.
I have the form setup on my contact.php page, yet when I enter the values and submit, no values are shown on the welcome.php page, which is where it should appear.
Here is my HTML code:
<form action="welcome.php" method="post">
<p>Your Name: <input type="text" name="yourname" /> </p><br/>
<p>E-mail: <input type="text" name="email" /></p>
<p>Do you like this website?</p>
<input type="radio" name="likeit" value="Yes" checked="checked" /> Yes
<input type="radio" name="likeit" value="No" /> No
<input type="radio" name="likeit" value="Not sure" /> Not sure</p>
<p>Your comments:<br />
<textarea name="comments" rows="10" cols="40"></textarea></p>
<p><input type="submit" value="Send it!"></p>
</form>
And here is my PHP code:
<html>
<body>
Your name is: <?php echo $_POST['yourname']; ?><br />
Your e-mail: <?php echo $_POST['email']; ?><br />
<br />
Do you like this website? <?php echo $_POST['likeit']; ?><br />
<br />
Comments:<br />
<?php echo $_POST['comments']; ?>
<br>
<br>
<br>
<button><-- BACK</button>
</body>
</html>
NOTE: - I am following a tutorial from http://myphpform.com/php-form-tutorial.php so I do not know why it is not working for me. If you could help then that would be truely amazing.
Thanks, DTV
Related
i want get the values of form in other php file but i am getting only one input value as the name are same in loop....How to get all the input values with the same name or if i have to change the name tell me how? and how to get it in other php file
<?php
if(isset($_POST['submit'])){
//getting values of number of MCQ questions and true/false questions
$mcq = $_POST['mcq'];
$truefalse = $_POST['truefalse'];
$number = 1;
echo '<form action="finalquestions.php" method="post">';
//loop to get inputs of mcq as many as the user posted
for($i=1;$i<=$mcq;$i++){
echo 'Question:'.$number.'<input type="text" name="question1" /> </br></br>';
echo '<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>';
echo '</br></br>';
$number++;
}
//loop to get inputs of truefalse as many as the user posted
for($i=1;$i<=$truefalse;$i++){
echo 'Question:'.$number.' <input type="text" name="question2" /> </br></br>';
echo '<input type="radio" name="question2">True</br>
<input type="radio" name="question1">False</br>';
echo '</br></br>';
$number++;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>WebQuiz Generator</title>
</head>
<body>
<input type="submit" name="submit" value="Generate Quiz" />
</form>
</body>
</html>
I am having issues with a piece of code in Joomla. It may be something to do with the plugin that enables the PHP but in case it isn't.
Page 1 has a form
<form action="/index.php/bridge" method="POST" name="postcode">
<div><input style="height: 50px;" type="text" placeholder="Enter Your Postcode..." /> <input type="submit" value="Get Started today!" /></div></form>
The text you input becomes the variable I want to pass over
On Page 2
<?php echo "test";
$postcode=1;
$poster=$_POST['postcode'];
echo $poster;
// You can place PHP like this
?>
Unfortunately, the postcode isn't echoed
Assuming nothing else is the cause of this error, try naming the input you are sending over to postcode:
<form action="/index.php/bridge" method="POST">
<div>
<input style="height: 50px;" name="postcode" type="text" "placeholder="Enter Your Postcode..." />
<input type="submit" value="Get Started today!" />
</div>
</form>
In your PHP code you are echoing $_POST['postcode'] but you are not sending the same variable at the time of form submission from input attribute.
<form action="/index.php/bridge" method="POST" name="demoForm">
<div>
<input style="height: 50px;" name="postcode" type="text" "placeholder="Enter Your Postcode..." />
<input type="submit" value="Get Started today!" />
</div>
</form>
Try this
<form action="/index.php/bridge" method="POST">
<div>
<input name="postcode" style="height: 50px;" type="text" placeholder="Enter Your Postcode..." />
<input type="submit" name="submit" value="Get Started today!" />
</div>
<?php
if (isset($_POST['submit'])){
echo $poster = $_POST['postcode'];
}else{
echo $poster = 1;
}
?>
I'm totally new to php and I am trying to dynamically change the form displayed when the user makes a choice on a drop down list. I can get the drop down list to show but it does not display anything when I submit.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Forms</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
If (!isset($_GET['userChoice']))
{ //open the if block to write the html to the screen
?>
<h1>Show your html option form</h1>
<form method="GET" action="thirdtry.php" name="userChoice">
<select size="1" id="choice" name="choice">
<option selected="selected">Choose which form you would like</option>
<option value="feedback">Feedback Form </option>
<option value="help">Help Request Form</option>
</select>
<input type="submit">
</form>
<?php
}//close the if block
else{ //show feedback
if($_GET['userChoice']=="feedback")
{
?>
<h1>Show the feedback form</h1>
<form id="fb" name="fb" method="post" >
<fieldset>
<label>First Name:
<br />
<input type="text" name="fName" id="fName" />
</label>
<br />
<label>Last Name:
<br />
<input type="text" name="lName" id="lName" />
</label>
<br />
<label>Street Address:
<br />
<input type="text" name="address" id="address" />
</label>
<br />
<label>City
<br />
<input type="text" name="city" id="city" />
</label>
<br />
<label>State
<br />
<input type="text" name="state" id="state" />
</label>
<br />
<label>Postcode
<br />
<input type="text" name="postcode" id="postcode" />
</label>
<br />
<label>Country
<br />
<input type="text" name="country" id="country" />
</label>
<br />
<label>Email Address:
<br />
<input type="text" name="FromAddress" id="FromAddress" />
</label>
<br />
<label>Your message:
<br />
<textarea name="message" rows="7" cols="30"></textarea> <br />
</label>
<div>How many stars would you give this site? <br />
(We love 5 stars!)<br />
<input type="radio" name="rating" value="1" />1<br />
<input type="radio" name="rating" value="2" />2<br />
<input type="radio" name="rating" value="3" />3<br />
<input type="radio" name="rating" value="4" />4<br />
<input type="radio" name="rating" value="5" />5<br />
</div>
<br />
<input type="submit" />
<input type="reset" />
</fieldset>
</form>
<?php
}
else
{//must be help form
?>
<h1>Show the help form</h1>
<form id="help" method="post" >
<fieldset>
<label>First Name:
<br />
<input type="text" name="fNameHelp" id="fNameHelp" />
</label>
<br />
<label>Last Name:
<br />
<input type="text" name="lNameHelp" id="lNameHelp" />
</label>
<br />
<label>Street Address:
<br />
<input type="text" name="addressHelp" id="addressHelp" />
</label>
<br />
<label>City
<br />
<input type="text" name="cityHelp" id="cityHelp" />
</label>
<br />
<label>State
<br />
<input type="text" name="stateHelp" id="stateHelp" />
</label>
<br />
<label>Postcode
<br />
<input type="text" name="postcodeHelp" id="postcodeHelp" />
</label>
<br />
<label>Country
<br />
<input type="text" name="countryHelp" id="countryHelp" />
</label>
<br />
<label>Email Address:
<br />
<input type="text" name="FromAddress" />
</label>
<br />
<label>Your message:
<br />
<textarea name="messageHelp" rows="7" cols="30"> </textarea> <br />
</label>
<div>How Would you like to be contacted?<br />
(Choose all that apply) <br />
<input type="checkbox" name="ckemail" id="ckemail" value="email" onclick="return validateHelpForm()"/>email<br />
<input type="checkbox" name="cktelephone" id="cktelephone" value="telephone" onclick="return validateHelpForm()"/>telephone<br />
</div>
<br />
<input type="submit" />
<input type="reset" />
</fieldset>
</form>
<?php
}//close the if block
}
?>
</body>
</html>
You're checking for the wrong variable:
If (!isset($_GET['userChoice']))
should be
If (!isset($_GET['choice']))
Also, why are you capitalizing If? (That won't cause an issue - it's just weird ;)
Well first off, do you want the page to refresh, or would you like it to change dynamically without a refresh, using Javascript. From your brief description, I am thinking you want the change to happen without a refresh.
And being a "novice", I would advise you use Jquery as your Framework as opposed to full blown javascript. Here is what I think you're trying to accomplish.
Since PHP is a server-side programming language, you can't dynamically change a webpage without a refresh. Javascript and Jquery are client-side, so many things can happen in your browser without the need or a refresh.
LINK: Here is the example I just created.
////////jquery/javascript///////
$('#changeme').change(function(){
var value = $('#changeme').val();
if(value == 1){
$('#dis1').show();
$('#dis2').hide();
}
else if(value == 2){
$('#dis1').hide();
$('#dis2').show();
}
else{
$('#dis1').hide();
$('#dis2').hide();
}
});
////////HTML///////
<select id="changeme">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<div style="display:none;" id="dis1">Displaying 1</div>
<div style="display:none;" id="dis2">Displaying 2</div>
Ok so Im trying to use radio buttons in my PHP code. I wrote a Madlib in PHP and I want to write in a radio button that when a user selects it, it will change the story etc. Happy ending or sad ending here the code I have so far. Any help would be appreciated.
<html>
<head>
<title>James Nygaard's Mablib</title>
</head>
<body bgcolor="<?php echo $bg; ?>" text="<?php echo $fg; ?>">
<h1 style="font-family:cursive;">Create your Madlib below:</h1>
<form action="" method="post">
Enter the name of a boy:
<input name="noun1" type="text" value="<?= $_POST['noun1'] ?>" /><br />
Enter an Adjective that describes a person:
<input name="adj1" type="text" value="<?= $_POST['adj1'] ?>" /><br />
Enter the name of a man:
<input name="noun2" type="text" value="<?= $_POST['noun2'] ?>" /><br />
Enter an Adjective that describes a person:
<input name="adj2" type="text" value="<?= $_POST['adj2'] ?>" /><br />
Enter the name of a woman:
<input name="noun3" type="text" value="<?= $_POST['noun3'] ?>" /><br />
Enter your favorite animal:
<input name="noun4" type="text" value="<?= $_POST['noun4'] ?>" /><br />
Enter a name:
<input name="noun5" type="text" value="<?= $_POST['noun5'] ?>" /><br />
Enter the name of your favorite city:
<input name="noun6" type="text" value="<?= $_POST['noun6'] ?>" /><br />
Enter a feeling that ends in "ness":
<input name="adj3" type="text" value="<?= $_POST['adj3'] ?>" /><br />
Enter a Verb that ends in "ing":
<input name="verb2" type="text" value="<?= $_POST['verb2'] ?>" /><br />
Enter the name of a boy:
<input name="noun7" type="text" value="<?= $_POST['noun7'] ?>" /><br />
Enter the name of a girl:
<input name="noun8" type="text" value="<?= $_POST['noun8'] ?>" /><br />
<input type="submit" value="Click here or press enter to see your MadLib" /><br />
</form>
<div style="color: #2F4F4F; font-family: cursive;">
<?php
$bg = "8FBC8F";
$fg = "2F4F4F";
if(isset($_POST['noun1'])) {
echo "<h1> The adventures of {$_POST['noun1']}. </h1>";
echo "This is where i will put the story. I already have the story but the code is really long so I left it out of this question.";
echo "<br> The end.";
}
?>
</div>
</body>
</html>
It's not especially clear in this small space, but I tried to follow your code style.
Select an option:
<label>
<input name="some_option" type="radio" value="option_1" <?php if(isset($_POST['some_option']) && $_POST['some_option'] == 'option_1') echo 'checked="checked" '; ?>/> Option 1
</label>
<label>
<input name="some_option" type="radio" value="option_2" <?php if(isset($_POST['some_option']) && $_POST['some_option'] == 'option_2') echo 'checked="checked" '; ?>/> Option 2
</label>
If you run that code you should see what's going on.
I've recently developed a website for a freelance client of mine, and within their website is a Contact Form and a Request for Estimate form.
Once a day, both forms are being submitted and emailed to the designated email address. However, the submitted forms are clearly not from a real user, this is because all of the fields contain the number 1. For example, the name field will be Name:1, and the address field will be Address:1. The number 1 is repeated for all input text fields, and even radio and check box form fields.
Below is a copy of the PHP file that I am using to submit the Request for Estimate form.
<?
$subject="Associated Sennott Contractors Request For Estimate From:".$_GET['firstname'];
$headers= "From: ".$_GET['email']."\n";
$headers.='Content-type: text/html; charset=iso-8859-1';
mail("email#gmail.com", $subject, "
<html>
<head>
<title>Associated Sennott Contractors Request For Estimate</title>
</head>
<body>
<p><strong>Associated Sennott Contractors Request For Estimate</strong></p>
<p>
First Name: ".$_GET['firstname']." <br />
Last Name: ".$_GET['lastname']." <br />
Company Name: ".$_GET['company']." <br />
Address 1: ".$_GET['address1']." <br />
Address 2: ".$_GET['address2']." <br />
City: ".$_GET['city']." <br />
State: ".$_GET['state']." <br />
Zip: ".$_GET['zip']." <br />
Phone: ".$_GET['phone']." <br />
Fax: ".$_GET['fax']." <br />
Email: ".$_GET['email']." <br /><br />
<strong>Property Type:</strong><br />
Residential Single Family: ".$_GET['singlefamily']." <br />
Residential Multi-Family: ".$_GET['multifamily']." <br />
Residential Out-Building : ".$_GET['outbuilding']." <br />
Commercial Office: ".$_GET['commercial']." <br />
Retail Store: ".$_GET['retail']." <br />
Restaurant: ".$_GET['restaurant']." <br />
Industrial Building: ".$_GET['industrial']." <br /><br />
<strong>Requested Services:</strong><br />
Fire, Water or Wind Damage Restoration: ".$_GET['restoration']." <br />
Scope of Loss Estimate to Insurance Company: ".$_GET['scope']." <br />
Smoke Odor Remediation: ".$_GET['smoke']." <br />
Exterior Remodeling or Siding: ".$_GET['exterior']." <br />
Interior Remodeling: ".$_GET['interior']." <br />
Hardwood and Laminate Flooring: ".$_GET['flooring']." <br />
Finish Carpentry: ".$_GET['carpentry']." <br />
Demolition and Debris Removal: ".$_GET['demo']." <br />
Exterior Decks, Patios and Fencing: ".$_GET['patio']." <br />
Other: ".$_GET['other']." <br /><br />
<strong>Additional Information:</strong><br />
Message: ".$_GET['info']."
</p>
</body>
</html>" , $headers);
header( 'Location: thankyou.html' ) ;
?>
You can also view the PHP code by follow the link here: http://sennottcontractors.com/home-repair-estimate/quote-code.html
You can then view the HTML code for the actual form below:
<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Request An Estimate Form</title>
<script type="text/javascript">
function formSubmit()
{
document.getElementById("estimate-form").submit();
}
</script>
</head>
<body>
<fieldset>
<legend><h3>Request a Home Restoration Estimate</h3></legend>
<form id="estimate-form" name="estimate-form" target="_parent" method="get" action="quote.php" onsubmit='return formValidator()'>
<p><strong>Contact Information</strong></p>
<p>First Name: *<br />
<input type="text" size="40" name="firstname" id="firstname" /></p>
<p>Last Name: *<br />
<input type="text" size="40" name="lastname" id="lastname" /></p>
<p>Company Name:<br />
<input type="text" size="40" name="company" id="company" /></p>
<p>Address 1: *<br />
<input type="text" size="40" name="address1" id="address1" /></p>
<p>Address 2:<br />
<input type="text" size="40" name="address2" id="address2" /></p>
<p>City: *<br />
<input type="text" size="30" name="city" id="city" /></p>
<p>State: *<br />
<input type="text" size="5" name="state" id="state" /></p>
<p>Zip: *<br />
<input type="text" size="10" name="zip" id="zip" /></p>
<p>Phone: *<br />
<input type="text" size="20" name="phone" id="phone" /></p>
<p>Fax:<br />
<input type="text" size="20" name="fax" id="fax" /></p>
<p>Email: *<br />
<input type="text" size="40" name="email" id="email" /></p>
<br />
<p><strong>Property Type</strong> *</p>
<p><input type="checkbox" name="singlefamily" id="singlefamily"/> Residential Single Family</p>
<p><input type="checkbox" name="multifamily" id="multifamily"/> Residential Multi-Family <em>(Condominium, apartment, town house, ect)</em></p>
<p><input type="checkbox" name="outbuilding" id="outbuilding"/> Residential Out-Building <em>(Garage, shed, ect)</em></p>
<p><input type="checkbox" name="commercial" id="commercial"/> Commercial Office</p>
<p><input type="checkbox" name="retail" id="retail"/> Retail Store</p>
<p><input type="checkbox" name="restaurant" id="restaurant"/> Restaurant</p>
<p><input type="checkbox" name="industrial" id="industrial"/> Industrial Building</p>
<br />
<p><strong>Requested Services</strong> *</p>
<p><input type="checkbox" name="restoration" id="restoration"/> Fire, Water or Wind Damage Restoration</p>
<p><input type="checkbox" name="scope" id="scope"/> Scope of Loss Estimate to Insurance Company</p>
<p><input type="checkbox" name="smoke" id="smoke"/> Smoke Odor Remediation</p>
<p><input type="checkbox" name="exterior" id="exterior"/> Exterior Remodeling or Siding</p>
<p><input type="checkbox" name="interior" id="interior"/> Interior Remodeling</p>
<p><input type="checkbox" name="flooring" id="flooring"/> Hardwood and Laminate Flooring</p>
<p><input type="checkbox" name="carpentry" id="carpentry"/> Finish Carpentry</p>
<p><input type="checkbox" name="demo" id="demo"/> Demolition and Debris Removal</p>
<p><input type="checkbox" name="patio" id="patio"/> Exterior Decks, Patios and Fencing</p>
<p><input type="checkbox" name="other" id="other"/> Other</p>
<br />
<p><strong>Additional Information</strong><br />
Please provide any information regarding details of your home restoration project or additional information to your requested services.</p>
<p><textarea rows="10" cols="65" id="info" name="info"></textarea></p>
<button type="submit" id="submit" onclick="formSubmit()">Submit</button>
</form>
<p>* Required Fields</p>
</fieldset>
</body>
</html>
The Request an Estimate form that is using the PHP file and code mentioned above you can view by following the link here: http://sennottcontractors.com/home-repair-estimate/index.html
Again, both the Request an Estimate form and the Contact Form are being submitted once a day, everyday, with the number 1 in every form field.
My guess is that this may be an issue with the PHP file itself, or it may be an issue from the server side of the hosted website.
Please help!!!
There's nothing technically wrong with your form. You said the cause of the issue yourself: "... the submitted forms are clearly not from a real user ...". So the solution is to make your form anti-bot. See this question on the pro Webmasters site for how you can do this: Make your site anti-bot?
One part of this problem is that you need to use a form nonce or "token".
Form.php
<?php
session_start();
$_SESSION['token'] = md5(mt_rand() . unique_id('form', TRUE));
...
?>
<form>
<input type="hidden" value="<?php print $_SESSION['token']; ">
...
</form>
process.php
<?php
session_start();
if($_SESSION['token'] !== $_POST['token'])
{
die('They did not load the form!');
}
...
(validation)
...
$db->insert($record);
You're not doing any validation. What do you expect?
Anyone could just grab the form fields, create the URL (because you're using GET instead of POST), and submit it ad nauseum if they wanted to. What you're probably encountering is a bot trying to figure out if it can hijack your form to send emails where it wants to send them.
What you should do is switch to using POST and check the $_SERVER['HTTP_REFERER'] variable to make sure it's coming from your form (at least). You could also use a CAPTCHA, but those are becoming increasingly unreliable. You could take this further and use a validation class to set rules for each field and what kind of data is allowed to be in each one.
Forms are easily manipulated, so if you expect to have any integrity in your form submissions, you should be doing the validation on the server-side. Client-side validation doesn't hurt, but only use it for user experience purposes, not to ensure data integrity.