php post superglobal array doesn't contain all keys from form - php

I have this html form in my php script
<form action="" method="post">
<div style="width: 30em;">
<label for="firstName">First name *</label>
<input type="text" name="firstName" id="firstName" value=""/>
<label for="lastName">Last name *</label>
<input type="text" name="lastName" id="lastName" value=""/>
<label for="password1">Choose a password *</label>
<input type="password" name="password1" id="password1" value=""/>
<label for="password2">Retype password *</label>
<input type="password" name="password2" id="password2" value=""/>
<label>Your gender: *</label>
<label for="genderMale">Male</label>
<input type="radio" name="gender1" id="genderMale" value="M" />
<label for="genderFemale">Female</label>
<input type="radio" name="gender" id="genderFemale" value="F" />
<label for="favoriteWidget">What's your favorite widget? *</label>
<select name="favoriteWidget" id="favoriteWidget" size="1">
<option value="superWidget">The SuperWidget</option>
<option value="megaWidget">The MegaWidget</option>
<option value="wonderWidget">The WonderWidget
</option>
</select>
<label for="newsletter">Do you want to receive our newsletter?</label>
<input type="checkbox" name="newsletter" id="newsletter" value="yes"/>
<label for="comments">Any comments?</label>
<textarea name="comments" id="comments" rows="4" cols="50"></textarea>
<div style="clear: both;">
<input type="submit" name="submitButton" id="submitButton" value="Send Details"/>
<input type="reset" name="resetButton" id="resetButton" value="Reset Form" style="margin-right: 20px;"/>
</div>
</div>
</form>
Problem is when i submit my form and run this php code:
if ( isset($_POST["submitButton"]) ) {
echo '<pre>' . print_r($_POST, true) . '</pre>';
}
I get the following results:
Array
(
[firstName] =>
[lastName] =>
[password1] =>
[password2] =>
[favoriteWidget] => superWidget
[comments] =>
[submitButton] => Send Details
)
Most noticeably the gender and newsletter keys in the $_POST array are missing?
What could be the reason for this?

All checkboxes are boolean for browsers.
This is not about PHP but HTML.
to verify that the person chose newsletter field, try this:
$_POST['newsletter'] = isset($_POST['newsletter']) ? $_POST['newsletter'] : 'no';
This force your $_POST set newsletter when form not checked!
isset() verify if your variable exists, then put default value else 'no' for this value.
you can use array_key_exists() instead of isset().
$_POST['newsletter'] = array_key_exists('newsletter', $_POST) ? $_POST['newsletter'] : 'no';

Related

Undefined index when I try to get a value from a select tag in HTML using PHP

I'm trying to get a value from a select tag in html using PHP but it keeps saying that the index is undefined.
My HTML form:
<form method="POST" action="proceso.php">
<label for="language">Idioma</label>
<select id="language" name="prueba">
<option value="es_LA">Español</option>
<option value="en_US">English</option>
</select>
<br>
<label for="username">Nombre de usuario</label>
<input type="text" name="username" id="username">
<br>
<label for="password">Contraseña</label>
<input type="password" name="password" id="password">
<br>
<label for="email">Correo electrónico</label>
<input type="email" name="email" id="email">
<br>
<label for="genderM">Masculino</label>
<input type="radio" name="gender" id="genderM" value="M">
<label for="genderF">Femenino</label>
<input type="radio" name="gender" id="genderF" value="F">
<label for="genderO">Otro</label>
<input type="radio" name="gender" id="genderO" value="O">
<br>
<button type="submit">Enviar</button>
</form>
And this is how I'm trying to get the value in my procesos.php file:
$sql = "INSERT INTO account(username,userpass,email,gender) VALUES('$_POST[prueba],'$_POST[username]','$_POST[password]',$_POST[email]',$_POST[gender]')";
The error says: Notice: Undefined index: prueba
On "INSERT INTO account(username,userpass,email,gender)" Here you are accepting 4 variable but you are passing '$_POST[prueba]','$_POST[username]','$_POST[password]','$_POST[email]','$_POST[gender]' Five to the query

html form-- need help having new input area populate if radio button is clicked

I am making a basic new customer database with mysql and php. I would like when people click on the radio button"different mailing address" for another couple of input fields to appear for the mailing address. Im not quite sure how to handle this with inputs and not variables. Is there a way to do an if statement here is my html form code below
<form method="POST" action="insert.php">
<fieldset>
<legend>New Customer data</legend>
<label>Complete Below</label>
<div class="controls controls-row">
<input class="span4" name="firstname" type="text" placeholder="First name">
<input class="span3" name="lastname" type="text" placeholder="Last Name">
<input class="span3" name="phone" type="text" placeholder="Phone">
</div>
<div class="controls controls-row">
<input class="span4" name="address" type="text" placeholder="Address">
<input class="span2" name="city" type="text" placeholder="City">
<input class="span1" name="state" type="text" placeholder="State">
</div>
<div class="controls controls-row">
<input class="span4" name="zip" type="text" placeholder="Zip Code">
<input class="span2" name="email" type="text" placeholder="Email">
<input class="span2" name="ccemail" type="text" placeholder="cc email">
</div>
<span class="help-block">When is the customers due date monthly</span>
<label class="radio">
<input type="radio" name="duedate" id="optionsRadios1" value="1" checked>
1st of the month</label>
<label class="radio">
<input type="radio" name="duedate" id="optionsRadios2" value="15">
15th of the month
</label>
<span class="help-block">Mailing address</span>
<label class="radio">
<input type="radio" name="mailingaddress" id="optionsRadios3" value="1" checked>
Check if mailing address is the same as the service address
</label>
<label class="radio">
<input type="radio" name="mailingaddress" id="optionsRadios4" value="0">
Check if mailing address is different
</label>
<button type="submit" class="btn btn-primary">Submit</button>
</fieldset>
</form>
Using jQuery, have two inputs that are initially hidden. Once the user checks the box, '$.show()' on those two inputs. Id have a div containing two inputs, and just show or hide the div depending on whether the box is checked or not
you can try this.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" >
</script>
<script>
$(document).ready(function(){
$("#checkme").on('change',function(){
if($("#checkme").is(":checked")){
$("input#myemail").fadeIn();
}else{
$("input#myemail").fadeOut();
}
});
});
</script>
</head>
<body>
show: <input type="checkbox" id="checkme" /><br />
<input type="text" id="myemail" hidden />
</body>
</html>

No new line before form

First my code:
<?php
echo 'Hello
<FORM ACTION="uebung3.php" METHOD="post">
<P>
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</P>
</FORM>
';
?>
So if i run that on my xampp-Server, it shows a "Hello" and the Form in a new line.
What must I do that all this is written in one line?
Thanks
You need to remove the <p> element and display the form inline.
<?php
echo 'Hello
<FORM ACTION="uebung3.php" METHOD="post" style="display:inline">
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
';
?>
Remove the paragraph break '<P>'
You should really use a heredoc for this type of HTML output in PHP. Technically, the <p> tag should be around the Hello, not the form. You're looking for something like:
<?php
echo <<<EOT
Hello
<FORM ACTION="uebung3.php" METHOD="post">
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
EOT;
?>
Even by removing <p> tags, your hello will still appear on a seperate line. This is because it is outside your <form> tag.
Put it inside <form> like this, while removing the <p> tags:
<?php
echo '<FORM ACTION="uebung3.php" METHOD="post">Hello
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
';
?>

Using the same script multiple times for one form (using PHP)

I am relatively new to PHP and programming, so please pardon my ignorance and language.
I have a form that has multiple instances of the same input fields (see below). I have a script that will process the input data, but only if it is submitted one set at a time(also below). I will like to have one submit button at the button of the form that would execute the script for however many multiples of the data set I have on the page.
Does anyone have any idea how I can get this to work? I cannot rename the variables (I tried that) because the receiving server needs the data labels to appear exactly as they are below. Any help/direction would be greatly appreciated. Thanks!
My form:
<form method="post" action="<?php echo (AUTHORIZENET_SANDBOX ? AuthorizeNetDPM::SANDBOX_URL : AuthorizeNetDPM::LIVE_URL)?>" id="checkout_form">
<?php
$time = time();
$fp_sequence = $time;
$fp = AuthorizeNetDPM::getFingerprint(AUTHORIZENET_API_LOGIN_ID, AUTHORIZENET_TRANSACTION_KEY, $amount, $fp_sequence, $time);
$sim = new AuthorizeNetSIM_Form(
array(
'x_amount' => $amount,
'x_fp_sequence' => $fp_sequence,
'x_fp_hash' => $fp,
'x_fp_timestamp' => $time,
'x_relay_response'=> "TRUE",
'x_relay_url' => $coffee_store_relay_url,
'x_login' => AUTHORIZENET_API_LOGIN_ID,
'x_test_request' => TEST_REQUEST,
)
);
echo $sim->getHiddenFieldString();
}
?>
<fieldset>
<H3>Card #1</H3>
<div>
<label>Amount</label>
<input type="text" class="text required" size="4" name="amount" value=""></input>
</div>
<div>
<label>Credit Card Number</label>
<input type="text" class="text required creditcard" size="15" name="x_card_num" value="6011000000000012"></input>
</div>
<div>
<label>Exp.</label>
<input type="text" class="text required" size="4" name="x_exp_date" value="04/15"></input>
</div>
<div>
<label>CCV</label>
<input type="text" class="text required" size="4" name="x_card_code" value="782"></input>
</div>
</fieldset>
<fieldset>
<div>
<label>First Name</label>
<input type="text" class="text required" size="15" name="x_first_name" value="John"></input>
</div>
<div>
<label>Last Name</label>
<input type="text" class="text required" size="14" name="x_last_name" value="Doe"></input>
</div>
</fieldset>
<fieldset>
<div>
<label>Address</label>
<input type="text" class="text required" size="26" name="x_address" value="123 Four Street"></input>
</div>
<div>
<label>City</label>
<input type="text" class="text required" size="15" name="x_city" value="San Francisco"></input>
</div>
</fieldset>
<fieldset>
<div>
<label>State</label>
<input type="text" class="text required" size="4" name="x_state" value="CA"></input>
</div>
<div>
<label>Zip Code</label>
<input type="text" class="text required" size="9" name="x_zip" value="94133"></input>
</div>
<div>
<label>Country</label>
<input type="text" class="text required" size="22" name="x_country" value="US"></input>
</div>
</fieldset>
<fieldset>
<H3>Card #2</H3>
<div>
<label>Amount</label>
<input type="text" class="text required" size="4" name="amount" value=""></input>
</div>
<div>
<label>Credit Card Number</label>
<input type="text" class="text required creditcard" size="15" name="x_card_num" value="6011000000000012"></input>
</div>
<div>
<label>Exp.</label>
<input type="text" class="text required" size="4" name="x_exp_date" value="04/15"></input>
</div>
<div>
<label>CCV</label>
<input type="text" class="text required" size="4" name="x_card_code" value="782"></input>
</div>
</fieldset>
<fieldset>
<div>
<label>First Name</label>
<input type="text" class="text required" size="15" name="x_first_name" value="John"></input>
</div>
<div>
<label>Last Name</label>
<input type="text" class="text required" size="14" name="x_last_name" value="Doe"></input>
</div>
</fieldset>
<fieldset>
<div>
<label>Address</label>
<input type="text" class="text required" size="26" name="x_address" value="123 Four Street"></input>
</div>
<div>
<label>City</label>
<input type="text" class="text required" size="15" name="x_city" value="San Francisco"></input>
</div>
</fieldset>
<fieldset>
<div>
<label>State</label>
<input type="text" class="text required" size="4" name="x_state" value="CA"></input>
</div>
<div>
<label>Zip Code</label>
<input type="text" class="text required" size="9" name="x_zip" value="94133"></input>
</div>
<div>
<label>Country</label>
<input type="text" class="text required" size="22" name="x_country" value="US"></input>
</div>
</fieldset>
<input type="submit" value="BUY" class="submit buy">
</form>
My script:
<?php
require_once 'coffee_store_settings.php';
if ($METHOD_TO_USE == "AIM") {
$transaction = new AuthorizeNetAIM;
$transaction->setSandbox(AUTHORIZENET_SANDBOX);
$transaction->setFields(
array(
'amount' => $amount,
'card_num' => $_POST['x_card_num'],
'exp_date' => $_POST['x_exp_date'],
'first_name' => $_POST['x_first_name'],
'last_name' => $_POST['x_last_name'],
'address' => $_POST['x_address'],
'city' => $_POST['x_city'],
'state' => $_POST['x_state'],
'country' => $_POST['x_country'],
'zip' => $_POST['x_zip'],
'email' => $_POST['x_email'],
'card_code' => $_POST['x_card_code'],
)
);
$response = $transaction->authorizeAndCapture();
if ($response->approved) {
// Transaction approved! Do your logic here.
header('Location: thank_you_page.php?transaction_id=' . $response->transaction_id);
} else {
header('Location: error_page.php?response_reason_code='.$response->response_reason_code.'&response_code='.$response->response_code.'&response_reason_text=' .$response->response_reason_text);
}
} elseif (count($_POST)) {
$response = new AuthorizeNetSIM;
if ($response->isAuthorizeNet()) {
if ($response->approved) {
// Transaction approved! Do your logic here.
// Redirect the user back to your site.
$return_url = $site_root . 'thank_you_page.php?transaction_id=' .$response->transaction_id;
} else {
// There was a problem. Do your logic here.
// Redirect the user back to your site.
$return_url = $site_root . 'error_page.php?response_reason_code='.$response->response_reason_code.'&response_code='.$response->response_code.'&response_reason_text=' .$response->response_reason_text;
}
echo AuthorizeNetDPM::getRelayResponseSnippet($return_url);
} else {
echo "MD5 Hash failed. Check to make sure your MD5 Setting matches the one in config.php";
}
}
PHP supports array notation for form fields, so you can force values to show up multiple times. e.g.
<input type="text" name="data[1]" value="foo" />
<input type="text" name="data[a]" value="bar" />
<input type="text" name="data[abcef]" value="baz" />
would give you a $_POST structure like:
$_POST['data'] = array(
0 => 'foo'
'a' => 'bar'
'abcef' => 'baz'
)
Since you'd have multiple copies of multiple fields, all requiring the data to be kept together, then structure your form like:
Set #1
<input name="somefield[1]" />
<input name="otherfield[1]" />
...
Set #2
<input name="somefield[2]" />
<input name="otherfield[2]" />
and then do this on the server upon submission:
foreach(array_keys($somefield) as $key) {
$somefield = $_POST['somefield'][$key];
$otherfield = $_POSt['otherfield'][$key];
... do something ...
}
You could add the form inputs in an array and use an foreach when you run the code.
<input type="text" class="text required" size="4" name="cards[0][amount]" value=""></input>
<input type="text" class="text required creditcard" size="15" name="cards[0][x_card_num]" value="6011000000000012"></input>
<input type="text" class="text required" size="4" name="cards[1][amount]" value=""></input>
<input type="text" class="text required creditcard" size="15" name="cards[1][x_card_num]" value="6011000000000012"></input>
etc etc etc.
You can get the results by doing
foreach($_POST['cards'] AS $c) {
echo $c['amount'];
}

Radio Buttons with PHP Form Handling

I have a basic form that I am submitting using some basic PHP. I have the form submission working great, except that I have a radio button (for preferred method of contact) and I am not sure how to add that in the PHP so that sends in the email. Both radio button options have the same name, so that isn't working as the value. My code is below.
The PHP is as follows:
<?php
$name = stripslashes($_POST['name']);
$email = stripslashes($_POST['email']);
$phone = stripslashes($_POST['phone']);
$contact = stripslashes($_POST['contact']);
$message = stripslashes($_POST['message']);
$form_message = "Name: $name \nEmail: $email \nPhone: $phone \nPreferred Method of Contact: $contact \nMessage: $message";
// Exit process if field "human" is filled (because this means it is spam)
if ( $_POST['human'] ) {
echo 'Tastes Like Spam!'; exit; }
// if it is not filled, submit form
else {
header( "Location: http://www.newurl.com");
mail("myemail#gmail.com", "Email Subject", $form_message, "From: $email" );
}
?>
The HTML for the form is below:
<form method="post" id="form" action="handle_form.php">
<div class="field">
<input type="text" name="human" id="human" class="txt" />
</div>
<div class="field form-inline">
<label class="contact-info" for="txtName">Name*</label>
<input type="text" name="name" id="name" class="txt" value=""/>
</div>
<div class="field form-inline">
<label class="contact-info" for="txtEmail">Email*</label>
<input type="text" name="email" id="email" class="txt" value=""/>
</div>
<div class="field form-inline">
<label class="contact-info" for="txtPhone">Phone</label>
<input type="text" name="phone" id="phone" class="txt" value=""/>
</div>
<div class="field form-inline radio">
<label class="radio" for="txtContact">Preferred Method of Contact</label>
<input class="radio" type="radio" name="contact" checked /> <span>Email</span>
<input class="radio" type="radio" name="contact" /> <span>Phone</span>
</div>
<div class="field form-inline">
<textarea rows="10" cols="20" name="message" id="message" class="txt" value=""></textarea>
</div>
<div class="submit">
<input class="submit" type="submit" name="submit" value="Submit Form">
</div>
</form>
Thanks so much for the help!
<div class="field form-inline radio">
<label class="radio" for="txtContact">Preferred Method of Contact</label>
<input class="radio" type="radio" name="contact" value="email" checked /> <span>Email</span>
<input class="radio" type="radio" name="contact" value="phone" /> <span>Phone</span>
</div>
Note the added value attribute.
And the PHP:
$contact = $_POST['contact']
//Will return either "email" or "phone".
You radios need values:
<input class="radio" type="radio" value="email" name="contact" checked /> <span>Email</span>
<input class="radio" type="radio" value="phone" name="contact" /> <span>Phone</span>
Just give your radio inputs a value-attribute. This is what will get submitted via POST. You can then access it via $_POST['nameofradio']
<input class="radio" type="radio" name="contact" value="Email" checked /> <span>Email</span>
<input class="radio" type="radio" name="contact" value="Phone" /> <span>Phone</span>
Easy! Just add a value to your radio buttons.
<input class="radio" type="radio" name="contact" value="Email" checked /> <span>Email</span>
<input class="radio" type="radio" name="contact" value="Phone" /> <span>Phone</span>

Categories