I have an HTML form that looks like this:
<form method="POST" action="ideal_test.php">
<input type="text" name="member[email]" placeholder="email" /><br />
<input type="text" name="member[country_code]" placeholder="country code" /><br />
<input type="text" name="member[first_name]" placeholder="first name" /><br />
<input type="text" name="member[last_name]" placeholder="last name" /><br />
<input type="text" name="member[street]" placeholder="street" /><br />
<input type="text" name="member[house_nr]" placeholder="house number" /><br />
<input type="text" name="member[houseNrExtension]" placeholder="house number addition" /><br />
<input type="text" name="member[postcode]" placeholder="postcode" /><br />
<input type="text" name="member[city]" placeholder="city" /><br />
<input type="text" name="member[phone]" placeholder="phone number" /><br />
<input type="text" name="member[birthday]" placeholder="birthday" /><br />
<input type="text" name="member[gender]" placeholder="gender" /><br />
<input type="text" name="member[ideal_bank]" placeholder="iDeal bank" /><br />
<input type="hidden" name="payment" value="ideal" />
<input type="submit" value="Test iDeal API call" />
The reason the form has been made this way is because I use an API that has to receive the data in this format. When I change the form action to the API URL, it works, however, sometimes I have to handle the form data myself with my own PHP and I don't know how I should process this form with PHP.
This is what I've tried:
$data = $_POST[ 'member' ];
foreach( $data AS $row ) {
echo $row . '<br />';
}
This shows all the data nicely separated by the <br />. However, I need to access each input separately, so I've tried this to achieve that:
foreach( $data AS $row ) {
echo $row['email'] . '<br />';
}
When I do this, it displays the first letter of each input, also separated by <br />. How can I loop through the member[] array and get every input separately?
$data is an array. So if you want to access each input separately You don't need to do it in foreach loop. You simply print it as normal array variable. Like this:
<?php
echo $data['email'];
echo $data['first_name'];
// Like this.
?>
Related
I have two pages in php the first contain a form which is:
<form method="post" action="addnames.php">
<input type="text" name="name" placeholder="Name" /><br />
<input type="text" name="name" placeholder="Name" /><br />
<input type="text" name="name" placeholder="Name" /><br />
<input type="text" name="name" placeholder="Name" /><br />
<input type="submit" value="Done" />
</form>
this takes the data to other php page where I am using foreach to read the request in this way:
foreach($_REQUEST['name'] as $name){
//MY CODE
}
So what is the problem?
If you want to get name as an array then you need to change your form code:
<form method="post" action="addnames.php">
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="submit" value="Done" />
</form>
Now you can get all names in your post request.
Because only one name is sent to the server, and it's a string then not an array. To send an array of names, change your input name to name="name[]" to identify it as an array
<input type="text" name="name[]" placeholder="Name" />
...
Try this code:
<?php
foreach($_REQUEST['name'] as $name){
//MY CODE
}?>
<form method="post" action="addnames.php">
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="submit" value="Done" />
</form>
I'm trying to cache the user input in my form. My code is:
echo '
<form method="POST">
Name:<br /><input class="contact_name" type="text" name="contact_name" maxlength="32" placeholder="Enter Name" /><br />
Email:<br /><input class="contact_email" type="text" name="contact_email" maxlength="50" placeholder="Email Address" /><br />
Subject:<br /><input class="contact_subject" type="text" name="contact_subject" maxlength="50" placeholder="Subject Line" /><br />
Message:<br /><textarea class="message_area" name="contact_message" rows="10" cols="50" maxlength="1000" placeholder="Message ..." /></textarea><br />
<input class="submit_button" name="submit_button" type="submit" value="Send" />
</form>
';
I tried searching for the answer and the only thing I found was adding:
<?php if(isset($contact_name)) { echo $contact_name; } ?>
This however does not work for me as my form is within a PHP echo and I'm trying to make a basic wordpress plugin. Whenever I bring the form outside the echo and , the style messes up and the form style itself breaks. So I was wondering if I can keep my form inside my echo along with a placeholder and be able to cache user inputs so when an error displays cause they didn't fill one of the spots out, it won't erase everything.
Thank you.
Then, just drop the echo and switch to HTML mode:
?>
<form method="post">
Name:<br /><input ... value="<?php echo (isset($contact_name) ? htmlspecialchars($contact_name, ENT_QUOTES, 'UTF-8') : ''; ?>" />
...
<?php
If you need this as a string, you could use output buffering:
ob_start();
?>
<input ... />
<?php
echo ob_get_clean();
if(isset($_POST['submit'])){
$domain=$_POST['domain'];
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$tel=$_POST['tel'];
if($domain==""){
$error="<h4>Enter Domain </h4>";
}elseif($fname == ""){
$error="<h4>Enter Firstname </h4>";
}elseif($sname == "")
{
$error="<h4 >Enter Surname</h4>";
}elseif($tel=="")
{
$error="<h4 >Enter telephono no</h4>";
}
else {
$sql11=mysql_query("INSERT INTO domain VALUES('','$domain','$fname','$sname','$tel','$mobile','$email','$company','$address','$city','$country','$pcode','$tele',
'$fax','$qus','$ans')");
echo $sql;
$db->query($sql);
}
}
<div><?php echo $error; ?></div>
<form action="" method="post" name="classic_form" id="classic_form">
<div><h4>Personal details:</h4></div><div style="margin-left: 109px;">
<div>Domain</div>
<input type="text" name="domain" id="domain" value="" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="" />
<div>Mobile:</div>
</form>
In my registration page, i used php validation. After the user submit the form if it shows validation errors it also resets all the fields. How can i resolve this problem? Without reset the fields i have to show the php validation errors. I also used in each input value. But it shows
"Notice: Undefined index: domain in D:\xampp\htdocs\deena\domainreg.php on line 82" . Please help me to resolve this problem
<input type="text" name="domain" id="domain" value="<?php echo isset($domain) ? $domain : ''; ?>" />
You have to pass all your values to php, and send back to html to feed your fields.
Its not 'resetting your fields' .. Your form is being submitted, hence the page is being reset and fields are therefore loading empty. Place the $_POST[] values in the field values upon page load:
<input type="text" name="domain" id="domain" value="<?php echo $domain ?>" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="<?php echo $fname?>" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="<?php echo $sname?>" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="<?php echo $tel?>" />
Simple. Just add the variables to the input values:
<input type="text" name="domain" id="domain" value="<?php echo $domain; ?>" />
You should also protect the outputted value, against cross site scripting:
<input type="text" name="domain" id="domain" value="<?php echo htmlspecialchars($domain); ?>" />
In the value field:
<input type="text" name="domain" id="domain"
value="<?php if(isset($_POST['domain'])){echo $_POST['domain'];} ?>">
Didn't test it. But i think it should work.
In input tag add the php value as like value="" So that it will echo if the variable is posted or it will show the empty one
It is easy to add more fields to a html form by jQuery. Then, we can serialize the fields, if the have the same name but what if we have a set of fields? For example
<input type="text" name="movie1_name" />
<input type="text" name="movie1_director" />
<input type="text" name="movie1_year" />
Now I want to add a new set of fields by jQuery as
<input type="text" name="movie2_name" />
<input type="text" name="movie2_director" />
<input type="text" name="movie2_year" />
and so forth. I process the form with PHP to insert movies into mysql database with three columns of (name, director, year). In the above-mentioned example, it is hard to serialize the fields to create appropriate $_POST arrays. How should I serialize jquery-added sets of movies?
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
Nothing else. On the server you will get (in case of POST) array in $_POST['movie_name'], $_POST['movie_director'] and $_POST['movie_year'];. Elements with the same index are from the same set of inputs.
What kind of problem with serialization do you have?
<form>
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<hr />
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<br />
<input type='button' id='serialize' value='Click me' />
</form>
and js code:
$('#serialize').click(function(){
alert($('form').serialize());
});
when you want to submit the data just write
$.post('script.php', $('form').serialize(), function() {alert('Saved');});
ps: if you are afraid to lose something, just compare count($_POST['movie_name']), count($_POST['movie_director']) and count($_POST['movie_year']).
or you can add indexes
<input type="text" name="movie_name[0]" />
<input type="text" name="movie_director[0]" />
<input type="text" name="movie_year[0]" />
<input type="text" name="movie_name[1]" />
<input type="text" name="movie_director[1]" />
<input type="text" name="movie_year[1]" />
Based on useful discussion with Cheery, I came to conclusion that the best and safest way is to use
<input type="text" name="movie_name[i]" />
<input type="text" name="movie_director[i]" />
<input type="text" name="movie_year[i]" />
where we define each i with jQuery to serialize the fields SAFELY. This way, we can be sure that serialized arrays are parallel and well matched, without misplacing.
You can do something like this:
<input type="text" name="movie1_name" />
<input type="text" name="movie1_director" />
<input type="text" name="movie1_year" />
// OTHER:
<input type="text" name="movie2_name" />
<input type="text" name="movie2_director" />
<input type="text" name="movie2_year" />
And do this to all... In Jquery, you create a function that create field as needed... I'm not the best in JQuery so I can't help you for this but the way I told you worked fine for me with PHP...
I've got a system where I allow a user to select multiple checkboxes from n amount of checkboxes, but also want two more inputs associated with each checkbox. This is for a message, a date and a time. When I post the data to be processed by a PHP script, I'd like to be able to access each of the sets of checkbox and two other inputs so I can see what date and time a user has filled in for each of the messages they've selected. I'm having trouble coming up with a method to associate the two other inputs with each checkbox.
Any ideas how to do this?
You can use arrays in your html inputs like so...
<input type="text" name="messages[1][message]" value="herp" />
<input type="text" name="messages[1][date]" value="24th April" />
<input type="text" name="messages[1][time]" value="13:00" />
<input type="text" name="messages[2][message]" value="derp" />
<input type="text" name="messages[2][date]" value="26th April" />
<input type="text" name="messages[2][time]" value="18:00" />
<?php
$messages = $_REQUEST['messages'];
foreach ($messages as $messageId => $value){
echo $value['message'];
echo $value['date'];
echo $value['time'];
}
Your HTML:
<input type="checkbox" name="car" />
<input type="text" name="msg_car" value="Car message" />
<input type="text" name="date_car" value="Car date" />
<input type="checkbox" name="bike" />
<input type="text" name="msg_bike" value="Bike message" />
<input type="text" name="date_bike" value="Bike date" />
<input type="checkbox" name="train" />
<input type="text" name="msg_train" value="Train message" />
<input type="text" name="date_train" value="Train date" />
<input type="checkbox" name="plane" />
<input type="text" name="msg_plane" value="Plane message" />
<input type="text" name="date_plane" value="Plane date" />
Your PHP script:
$array = array("car", "bike", "train", "plane");
for ($i = 0; $i < count($array); $i++) {
if (isset($_POST[$array[$i]])) {
//Checkbox was checked, get values
$msg = "";
$date = "";
$msg_id = "msg_" . $array[$i];
$date_id = "date_" . $array[$i];
if (isset($_POST[$msg_id]))
$msg = $_POST[$msg_id];
if (isset($_POST[$date_id]))
$date = $_POST[$date_id];
}
}
I think something like this should work. I didn't test it.. So forgive me if this example still contains some minor errors.