PHP error notice on radio button - php

I am validating a form using PHP (very very basic form) and im wondering if there is a solution to not get this error Notice: Undefined index: sendMethod in /directory/... on line 26
here is my code that i have for my form:
<form name="myForm" id="userDetails" action="formProcess.php" onSubmit="return validateForm()" method="post">
<fieldset class="custInf">
<h3> User Details </h3>
<label class="inputArea" for="fName">Forename :</label>
<input type="text" name="forename" id="fname" placeholder="Enter First Name" maxlength="20" size="15">
</input>
<label class="inputArea" for="sName">Surname :</label>
<input type="text" name="surname" id="surname" placeholder="Enter Last Name" maxlength="20" size="15">
</input>
<label class="inputArea" for="email">Email :</label>
<input type="email" name="email" id="email" placeholder="Enter Email Address" maxlength="40" size="15" />
</input>
<label class="inputArea" for="hmph">Landline number :</label>
<input type="tel" name="landLineTelNo" id="hmphone" placeholder="Enter Landline no." maxlength="11" size="15">
</input>
<label class="inputArea" for="mobileTelNo">Mobile number :</label>
<input type="tel" name="mobileTelNo" id="mobile" placeholder="Enter Mobile no." maxlength="11" size="15">
</input>
<label class="inputArea" for="address">Postal Address :</label>
<input type="text" name="postalAddress" id="address" placeholder="Enter House no." maxlength="25" size="15">
</input>
</fieldset>
<fieldset class="contactType">
<h3> How would you like to be contacted? </h3>
<label class="radioBt" for="sms">SMS</label>
<input id="smsBut" type="radio" name="sendMethod" value="SMS">
</input>
<label class="radioBt" for="email">Email</label>
<input type="radio" name="sendMethod" value="Email">
</input>
<label class="radioBt" for="post">Post</label>
<input type="radio" name="sendMethod" value="Post">
</input>
</fieldset>
<fieldset class="termsCon">
<input type="checkbox" name="check" value="check" id="check" />I have read and agree to the Terms and Conditions and Privacy Policy
</input>
</fieldset>
<input class="submitBut" type="submit" name="submitBut" value="Submit" </input>
</form>
$fname = $_REQUEST['forename'];
if (empty($fname)) {
die("<p>Enter a first name</p>\n");
}
$surname = $_REQUEST['surname'];
if (empty($surname)) {
die("<p>You must enter a surname</p>\n");
}
$email = $_REQUEST['email'];
if (empty($email)) {
die("<p>You need to enter an email</p>\n");
}
//Landline not required so wont give error
$hmphone = isset($_REQUEST['hmph']) ? $_REQUEST['hmph'] : null ;
$mobile = $_REQUEST['mobileTelNo'];
if (empty($mobile)) {
die("<p>Enter a Mobile Number</p>\n");
}
$address = $_REQUEST['postalAddress'];
if (empty($address)) {
die("<p>Enter your postal address</p>\n");
}
$sendMethod = $_REQUEST['sendMethod'];
if (empty($sendMethod)) {
die("<p>Please choose a contact option</p>\n");
}
$check = $_REQUEST['check'];
if (empty($check)) {
die("<p>Please select the Terms and Conditions to continue</p>\n");
}
its a very basic PHP validation, i just want it to not give me a error notice until all over fields have been checked for example, the error shows up once all the fields are filled in saying "Please choose a contact option" but if i fill out all of the fields bar the mobile, i get the error notice

to avoid the notice, check if it's empty first
if (empty($_REQUEST['sendMethod']))
die("<p>Please choose a contact option</p>\n");
else
$sendMethod = $_REQUEST['sendMethod'];

Related

Saving contact form data in a file by pressing a submit button doesn't seem to work

I made a contact form with a submit button. I want all the information saved which got typed into the contact form. Somehow the submit button does nothing.
In the past it seemed to work but as a php/html newbie I just cant seem to find the problem
HTML
<form method="post">
<div class="contact-form" action="contact-form.php" method="post">
<div class="contentLeft">
<h4>Ihre Daten</h4>
<br>
<h3>Vorname</h3>
<input class="inputText" type="text" name="firstName" required="required">
<br>
<h3>Nachname</h3>
<input class="inputText" type="text" name="lastName" required="required">
<br>
<h3>E-Mail</h3>
<input class="inputText" type="text" name="mail" required="required">
<br>
<h3>Straße</h3>
<input class="inputText" type="text" name="street" required="required">
<br>
<h3>Postleitzahl</h3>
<input class="inputText" type="text" name="postal" required="required">
<br>
<h3>Stadt</h3>
<input class="inputText" type="text" name="city" required="required">
<br>
<h3>Telefonnummer</h3>
<input class="inputText" type="number" name="phone" required="required">
<br>
</div>
<div class="contentRight">
<h4>Ihre Nachricht</h4>
<br>
<h3>Betreff</h3>
<input class="inputTextRight" type="text" name="subject" required="required">
<br>
<h3>Buchungscode</h3>
<input class="inputTextRight" type="text" name="bookingCode" required="required">
<br>
<br><
<textarea></textarea>
</div>
<br>
<button class="button" type="submit" name="submit">Absenden</button>
</div>
</form>
PHP
if (isset($_POST['submit'])) {
$firstName = $_POST['firstname'];
$lastName = $_POST['lastname'];
$mail = $_POST['mail'];
$street = $_POST['street'];
$postal = $_POST['postal'];
$city = $_POST['city'];
$phone = $_POST['phone'];
$subject = $_POST["subject"];
$bookingCode = $_POST['bookingCode'];
$message = $_POST['message'];
$data=$_POST["firstName"] ."\n".$_POST["firstName"] ."\n".$_POST["lastName"] ."\n".$_POST["mail"] ."\n".$_POST["street"] ."\n". $_POST["postal"] ."\n".$_POST["city"] ."\n".$_POST["phone"] ."\n". $_POST["subject"] ."\n".$_POST["bookingCode"] ."\n". $_POST["message"];
$fp = fopen("data.txt", "a");
fwrite($fp, $data);
fclose($fp);
header ("Location: NewTest.html?mailsent");
}
?>
You added action of form in div, move that from div to form.
<form action="contact-form.php" method="post">
<div class="contact-form">
<div class="contentLeft">
<h4>Ihre Daten</h4>
<br>
<h3>Vorname</h3>
<input class="inputText" type="text" name="firstName" required="required">
<br>
<h3>Nachname</h3>
<input class="inputText" type="text" name="lastName" required="required">
<br>
<h3>E-Mail</h3>
<input class="inputText" type="text" name="mail" required="required">
<br>
<h3>Straße</h3>
<input class="inputText" type="text" name="street" required="required">
<br>
<h3>Postleitzahl</h3>
<input class="inputText" type="text" name="postal" required="required">
<br>
<h3>Stadt</h3>
<input class="inputText" type="text" name="city" required="required">
<br>
<h3>Telefonnummer</h3>
<input class="inputText" type="number" name="phone" required="required">
<br>
</div>
<div class="contentRight">
<h4>Ihre Nachricht</h4>
<br>
<h3>Betreff</h3>
<input class="inputTextRight" type="text" name="subject" required="required">
<br>
<h3>Buchungscode</h3>
<input class="inputTextRight" type="text" name="bookingCode" required="required">
<br>
<br><
<textarea></textarea>
</div>
<br>
<button class="button" type="submit" name="submit">Absenden</button>
</div>
</form>

PHP form validation number fields

I need this field to only allow numbers to be input to the field and there must be 11 digits that have been input into the "Mobile Number" field, this is the form I have created
<form name="myForm" id="userDetails" action="formProcess.php" onSubmit="return validateForm()" method="post">
<fieldset class="custInf">
<h3> User Details </h3>
<label class="inputArea" for="fName">Forename :</label>
<input type="text" name="forename" id="fname" placeholder="Enter First Name" maxlength="20" size="15"></input>
<label class="inputArea" for="sName">Surname :</label>
<input type="text" name="surname" id="surname" placeholder="Enter Last Name" maxlength="20" size="15"></input>
<label class="inputArea" for="email">Email :</label>
<input type="email" name="email" id="email" placeholder="Enter Email Address" maxlength="40" size="15" /></input>
<label class="inputArea" for="hmph">Landline number :</label>
<input type="tel" name="landLineTelNo" id="hmphone" placeholder="Enter Landline no." maxlength="11" size="15"></input>
<label class="inputArea" for=" mobileTelNo">Mobile number :</label>
<input type="tel" name=" mobileTelNo" id="mobile" placeholder="Enter Mobile no." maxlength="11" size="15"></input>
<label class="inputArea" for="address">Postal Address :</label>
<input type="text" name="postalAddress" id="address" placeholder="Enter House no." maxlength="25" size="15"></input>
</fieldset>
<fieldset class="contactType">
<h3>
How would you like to be contacted?
</h3>
<label class="radioBt" for="sms">SMS</label>
<input id="smsBut" type="radio" name="sendMethod" value="SMS"></input>
<label class="radioBt" for="email">Email</label>
<input type="radio" name="sendMethod" value="Email"></input>
<label class="radioBt" for="post">Post</label>
<input type="radio" name="sendMethod" value="Post"></input>
</fieldset>
<fieldset class="termsCon">
<input type="checkbox" name="check" value="check" id="check" />I have read and agree to the Terms and Conditions and Privacy Policy</input>
</fieldset>
<input class="submitBut" type="submit" name="submitBut" value="Submit" </input>
Here is the PHP I have created so far to validate the user input `//Ensures user enters information required
$fname = $_REQUEST['forename'];
if (empty($fname)) {
die("<p>Enter a first name</p>\n");
}
$surname = $_REQUEST['surname'];
if (empty($surname)) {
die("<p>You must enter a surname</p>\n");
}
$email = $_REQUEST['email'];
if (empty($email)) {
die("<p>You need to enter an email</p>\n");
}
$hmphone = isset($_REQUEST['hmph']) ? $_REQUEST['hmph'] : null ;
$mobile = $_REQUEST['mobileTelNo'];
if (empty($mobile)) {
die("<p>Enter a Mobile Number</p>\n");
}
$address = $_REQUEST['postalAddress'];
if (empty($address)) {
die("<p>Enter your postal address</p>\n");
}
$sendMethod = $_REQUEST['sendMethod'];
if (empty($sendMethod)) {
die("<p>Please choose a contact option</p>\n");
}
$check = $_REQUEST['check'];
if (empty($check)) {
die("<p>Please select the Terms and Conditions to continue</p>\n");
}`
You can pattern match inside the form by adding the following code inside the relevant input field thanks to the html5 pattern tag. More details on the pattern tag here: http://www.w3schools.com/tags/att_input_pattern.asp
pattern="[0-9]{11}"
It's quite similar on the server side php checking thankfully!
$isValid = preg_match("/[0-9]{11}/", $formvalue);
$isValid will return true (1) if it matches or false (0) if it doesn't match.
preg_match is a very useful function in general for custom validation. There's more details on it available here: http://php.net/manual/en/function.preg-match.php
If you're sure your rule is correct you can do this:
if (empty($mobile) || preg_match('/^[0-9]{11}$/', $mobile) != 1 ) {
die("<p>Enter a Mobile Number</p>\n");
}
from the docs:
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
The regular expression explained:
^ assert position at start of the string
[0-9] match a single character present in the list below
Quantifier: {11} Exactly 11 times
0-9 a single character in the range between 0 and 9
$ assert position at end of the string
you can validate with this:
if(empty($number)) {
echo '<p> Please enter a value</p>';
} else if(!is_numeric($number)) {
echo '<p> Data entered was not numeric</p>';
} else if(strlen($number) != 11) {
echo '<p> The number entered was not 6 digits long</p>';
}

How to get the page title into a input's value?

I want my hidden input field to get the page title as its value.
This is my testmail.php code.
<p>From: <?php echo $_POST['name']; ?></p>
<p>Subject: <?php echo $_POST['subject']; ?></p>
<p>Email: <?php echo $_POST['email']; ?></p>
<p>Phone Number: <?php echo $_POST['phone']; ?></p>
<p style="width:300px;">Message: <?php echo $_POST['message']; ?></p>
and this is my form code
<form action="testmail.php" method="post" class="cf">
<label for="name">* Name:</label>
<input type="text" name="name" placeholder="Your Name">
<input type="hidden" name="subject" value="<?php value $_POST['pagetitle']; ?>">
<label for="email">* Email:</label>
<input type="text" name="email" placeholder="Enter a valid Email Address">
<label for="phone"> Phone:</label>
<input type="text" name="phone" placeholder="Enter areacode and number">
<label for="message">* Message:</label>
<textarea name="message"></textarea>
<button type="input">Send</button>
</form>
Is there a way to get the title automatically?
Here's a pure javascript way using the onsubmit event.
<form onsubmit="this.subject.value=document.title;">
<input type="text" name="subject" value="" />
<input type="submit" />
</form>
To give you a better understanding of the onsubmit event as the name suggests it executes the contained javascript when the user submits the form. The operation this.subject.value=document.title working from right to left basically says assign the value of document.title to the value attribute of the element with the name of subject in this specific form.
Using your existing form it should look like this (I added the onsubmit event and fixed up errors in your html as well as added the appropriate id's to form elements):
<form action="testmail.php" method="post" class="cf" onsubmit="this.subject.value=document.title;">
<label for="name">* Name:</label>
<input type="text" id="name" name="name" placeholder="Your Name" />
<input type="hidden" name="subject" value="" />
<label for="email">* Email:</label>
<input type="text" id="email" name="email" placeholder="Enter a valid Email Address" />
<label for="phone"> Phone:</label>
<input type="text" id="phone" name="phone" placeholder="Enter areacode and number" />
<label for="message">* Message:</label>
<textarea id="message" name="message"></textarea>
<input type="submit" name="submit" value="submit" />
</form>
This will set the value of your hidden input field once the page has finished loading.
<script>
$(function() {
$('input[name="subject"]').val($('title').text());
});
</script>
You can use JavaScript to do so, assuming you're using jQuery, bind submit event to your form
$('your_form_selector').on('submit', function(){
$('<input />').attr('type', 'hidden')
.attr('name', 'title')
.attr('value', document.title)
.appendTo($(this));
});

How to populate text fields when radio button is clicked?

I am creating a web page where there are two divs (billing details and shipping details). When the page is loaded, the billing details are automatically displayed and the shipping details remain empty. I have included two radio buttons which allows the user to choose whether or not the shipping details are the same as the billing details. If the user selects yes from the radio buttons then the same details should be displayed in the shipping details.
Note: the details are stored in database are am using php to get the data displayed
At the moment, I have only tried using
<?php if(isset($_POST'[shipping'] == 'yes')){echo $fname} ?>
on the first name field just to see if it is working, but i doesnt seem to work.
<div id="leftprofile">
<form id="au" method="post" action="../../../coursework/coursework/scripts/checkout.php">
<fieldset class="billing">
<legend>Billing Details</legend><br />
<label for="fname" class="reglabel">First Name:</label>
<input required name="fname" type="text" id="fname" value="<?php echo $fname ?>"/><br />
<label for="lname" class="reglabel">Last Name:</label>
<input required name="lname" type="text" id="lname" value="<?php echo $lname ?>"/><br />
<label for="address" class="reglabel">Address:</label>
<input required name="address" id="address" type="text" value="<?php echo $address ?>"/><br />
<label for="town" class="reglabel">Town:</label>
<input required name="town" id="town" type="text" value="<?php echo $town ?>"/><br />
<label for="postcode" class="reglabel">Post Code:</label>
<input required name="postcode" id="postcode" type="text" value="<?php echo $postcode ?>"/><br />
<label for="phone" class="reglabel">Phone:</label>
<input required name="phone" id="phone" type="text" value="<?php echo $phone ?>"/><br />
<label for="email" id="EmailLabel" class="reglabel">E-mail:</label>
<input required name="email" type="email" id="email" value="<?php echo $email ?>"/><br />
</fieldset>
</form>
</div>
<div id="rightprofile">
<form id="au" method="post" action="../../../coursework/coursework/scripts/checkout.php">
<fieldset class="billing">
<legend>Shipping Details</legend><br />
<form>
Same as billing address?
<input type="radio" name="shipping" id="yes" value="yes">Yes
<input type="radio" name="shipping" id="no" value="no">No<br/>
</form>
<label for="fname" class="reglabel">First Name:</label>
<input required name="fname" type="text" id="fname" value="<?php if(isset($_POST'[shipping'] == 'yes')){echo $fname} ?>"/><br />
<label for="lname" class="reglabel">Last Name:</label>
<input required name="lname" type="text" id="lname" /><br />
<label for="address" class="reglabel">Address:</label>
<input required name="address" id="address" type="text" /><br />
<label for="town" class="reglabel">Town:</label>
<input required name="town" id="town" type="text" /><br />
<label for="postcode" class="reglabel">Post Code:</label>
<input required name="postcode" id="postcode" type="text" /><br />
<label for="phone" class="reglabel">Phone:</label>
<input required name="phone" id="phone" type="text" /><br />
<label for="email" id="EmailLabel" class="reglabel">E-mail:</label>
<input required name="email" type="email" id="email" /><br />
</fieldset>
</form>
</div>
I have written you some simplified example code. This site contains one form, with two input fields (billing and shipping), and one checkbox to check if the shipping information is the same as the billing information. If the checkbox is checked the code will simply ignore anything typed into 'shipping'.
This would achieve what you are asking for, at least from a PHP perspective. If you are looking more for the "copy the data in those input fields, to those input fields" in real time in the browser, then that is a task for Javascript, and not PHP.
<?php
/* Check if anything was submitted */
if(isset($_POST))
{
/* Retrieve billing information */
$billing_name = $_POST['billing_name'];
$billing_addr = $_POST['billing_addr'];
/* Check if shipping same as billing */
if(isset($_POST['same']))
{
/* Shipping is the same as billing */
$shipping_name = $billing_name;
$shipping_addr = $billing_addr;
}
/* If not, set shipping to the posted value */
else
{
$shipping_name = $_POST['shipping_name'];
$shipping_addr = $_POST['shipping_addr'];
}
$insert = mysql_query(...);
}
?>
<form method="post" action="#" />
Billing information
<label for="billing_name">Name</label>
<input type="text" id="billing_name" name="billing_name" />
<label for="billing_addr">Addr</label>
<input type="text" id="billing_addr" name="billing_addr" />
<label for="same" />Is the shipping information the same as billing information?</label>
<input type="checkbox" id="same" name="same" />
Shipping information
<label for="shipping_name">Name</label>
<input type="text" id="shipping_name" name="shipping_name" />
<label for="shipping_addr">Addr</label>
<input type="text" id="shipping_addr" name="shipping_addr" />
<input type="submit" value="Register" />
</form>

Cannot store radio button value in table

I am trying to save the value of the radio buttons in my database table choice. I get the message Data saved successfully but no value is stored in the table.
Form:
<form id="myForm" method="post" action="">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<center<legend>Choose in which category you'd like to be included</legend></center>
<p><input type="radio" name="choice[]" value="player" id="player" class="custom" />
<label for="player">Player</label>
<input type="radio" name="choice[]" value="coach" id="coach" class="custom" />
<label for="coach">Coach</label>
<input type="radio" name="choice[]" value="supporter" id="supporter" class="custom" />
<label for="supporter">Supporter</label>
<input type="radio" name="choice[]" value="sponsor" id="sponsor" class="custom" />
<label for="sponsor">Sponsor</label>
<input type="radio" name="choice[]" value="alumni" id="alumni" class="custom" />
<label for="alumni">Alumni</label>
<input type="radio" name="choice[]" value="other" id="o" class="custom" />
<label for="o">Other</label>
</fieldset>
</div>
<div data-role="fieldcontain">
<label for="name">Please enter your name:</label>
<input type="text" name="name" id="name" class="required" value="" autocomplete="off" /><br />
<label for="email">Please enter your e-mail:</label>
<input type="text" name="email" id="email" value="" class="required" autocomplete="off" /><br />
<label for="phone">Please enter your phone number:</label>
<input type="number" name="phone" id="phone" value="" class="required" autocomplete="off" />
<br><br>
<label for="other">Other comments</label>
<textarea name="other" id="other" autocomplete="off" placeholder="Anything else you'd like to add?">
</textarea>
<p><strong id="error"></strong></p>
<br><br>
<input type="submit" id="save" name="save" value="Submit Form" />
<p id="response"></p>
</form>
</body>
</html>
PHP:
<?php
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if(isset($_POST['save']))
{
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);
$phone = $mysqli->real_escape_string($_POST['phone']);
$other = $mysqli->real_escape_string($_POST['other']);
$choice = $mysqli->real_escape_string($_POST['choice']);
$query = "INSERT INTO Players (`name`,`email`,`phone`,`other`,`choice`) VALUES ('".$name."','".$email."','".$phone."','".$other."','".$choice."')";
if($mysqli->query($query))
{
echo 'Data Saved Successfully.';
}
else
{
echo 'Cannot save data.';
}
}
?>
var_dump($_POST) to sere what data flooding in.
One thing more to check if its save or submit ? in $_POST['save']
EDIT
After getting your full form - the error lies in your center tag
Change <center<legend> TO <center><legend>
The error - ↑ in this tag

Categories