I am new to php programming language.So basically I am creating a simple form.
<?php
print_r($_POST);
?>
<form name="form1" method="post" action="">
Name: <input type="text" name="mail"><br>
Phone No: <input type="text" name="phon" /><br/>
Course:<input type="text" name="course" /> <br />
Experience: <select name="exp"> <option value="beginner">Beginner</option> <option value="intermediate">Intermediate</option> <option value="advanced">Advanced</option> </select><br>
<input type="submit" name="Submit" value="Sign Up">
</form>
So the output will be something like this:
Array ( [mail] => john [phon] => 123455666 [course] => bsc [exp] => beginner [Submit] => Sign Up )
I want to modify or change the output something like this,
Name=john
Phone No=123455666
Course=bsc
Experience=beginner
And I want to store that in the arrays, i.e. right hand side parameters in one array and left hand side parameters in another array. So that it will be easy to access or search the data. In the next level of this i want to save these values in a file.
Please help me out.
Any help or advice is appreciated. Thanks in advance.
$array1= array();
$array2= array();
foreach($_POST as $key => $value){
echo $key ."=". $value ;
echo "<br>";
$array1[]=$key; //first array for left hand side
$array2[]=$value; //second array for right hand side
}
print_r($array1); print_r($array2);
Output:-
phon=123456
course=maths
exp=intermediate
Submit=Sign Up
Array ( [0] => mail [1] => phon [2] => course [3] => exp [4] => Submit ) Array ( [0] => tet [1] => test [2] => stedt [3] => intermediate [4] => Sign Up )
Name of fields are key to $_POST array
$_POST['name_of_input']
Example:
$_POST['mail']
$_POST['phone']
Look at array_keys() and array_values()
http://php.net/manual/en/function.array-keys.php
http://php.net/manual/en/function.array-values.php
Take lessons on MySQL to store data in database
I suggest to set the names you want in the form on the first place.
Changing the $_POST is not a good practice. All it can do is confuse you and anyone else might read your code.
<form name="form1" method="post" action="">
Name: <input type="text" name="Name"><br>
Phone No: <input type="text" name="Phone No" /><br/>
Course:<input type="text" name="Course" /> <br />
Experience: <select name="Experience"> <option value="beginner">Beginner</option> <option value="intermediate">Intermediate</option> <option value="advanced">Advanced</option> </select><br>
<input type="submit" name="Submit" value="Sign Up">
</form>
Right Hand Side parameters:
$rhs_params = $_POST;
Left Hand Side parameters:
$lhs_params = array_keys($_POST);
I am not sure what you are planning to do with this tho!!
foreach($_POST as $key => $value){
#TO DO your operation
}
Related
I need to insert the value of form into the data table.
here is my form for taking a test.
<form method="post" action="">
1<input type="text" name="answer">
3<input type="text" name="answer">
5<input type="text" name="answer">
7<input type="text" name="answer">
<input type="submit" name="submit">
</form>
how to store this all answer in one field of the data table
i have tried this code but its not storing all value
if(isset($_POST['submit']))
{
$answer = json_encode($_POST['answer']);
$query = "INSERT INTO `test` (`answer`) VALUES ('$answer')";
mysqli_query($con, $query);
if (mysqli_query($con, $query)) {
echo "inserted ";
} else {
echo "Error updating record: " . mysqli_error($con);
}
}
Try changing your HTML code to this.
<form method="post" action="">
1<input type="text" name="answer[]">
3<input type="text" name="answer[]">
5<input type="text" name="answer[]">
7<input type="text" name="answer[]">
<input type="submit" name="submit">
</form>
When you submit the form the $_POST['answer'] would look something like this
Array
(
[answer] => Array
(
[0] => first answer
[1] => second answer
[2] => third answer
[3] => fourth answer
)
)
Above is a printout of $_POST variable like so print_r($_POST)
Now you can easily get all the answer inputs inside PHP and have it converted to json like so
$answer = json_encode($_POST['answer']);
However since you are saving this inside mysql I would suggest using serialize() instead of json_encode(). Please read about serialize() https://www.php.net/manual/en/function.serialize.php
I've used Respect/Validation successfully for my general concern.
But now I'm validating some form Input where the user can check multiple checkboxes and the data is send with an array.
The form looks something like this:
<form method="post" action="">
<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="checkbox" name="options[]" value="1">
<input type="checkbox" name="options[]" value="2">
<input type="checkbox" name="options[]" value="3">
<button type="submit">Send</button>
</form>
So, my post-data will look like this:
Array
(
[firstname] => Peter
[lastname] => Parker
[options] => Array
(
[0] => 1
[1] => 3
)
)
I've build a validation rule which works:
<?php
//used in class, so "use Respect\Validation\Validator AS v;"
$validReq = v::create()
->key('firstname', v::stringType()->length(1, 32))
->key('lastname', v::stringType()->length(1, 32))
->key('options', v::optional(v::arrayType()))
->setName('valid request');
My question now is, how do I validate the array options with (e.g.) v::intVal()?
Maybe I've just oversaw how to accomplish this. Thank you for your time.
Cheers,
Patrik
Solved with the help of alganet over at github.
This could be accomplished using each():
<?php
$validReq = v::create()
->key('firstname', v::stringType()->length(1, 32))
->key('lastname', v::stringType()->length(1, 32))
->key('options', v::optional(v::arrayType()->each(v::intVal())))
->setName('valid request');
Cheers,
Patrik
Sorry for the noob question. But I am stuck here.
This is my HTML form where the user-form div can be cloned to as many as possible. The #submit-form div has some hidden values which are common for all.
HTML -
<div class="user-form">
<input type="text" autocomplete="off" name="name[]" >
<input type="email" autocomplete="off" name="mail[]" >
</div>
<div class="user-form">
<input type="text" autocomplete="off" name="name[]" >
<input type="email" autocomplete="off" name="mail[]" >
</div>
<div id="submit-form">
<input type='hidden' name='refer_user_id' value='<?php echo $refer_user_id ?>'>
<input type='hidden' name='refer_user_email' value='<?php echo $refer_user_email ?>'>
<input type="submit" value="Invite" />
<input type="button" class="button" id="clonetrigger" value="Clone" />
</div>
I'm using ajax to submit the form. Basically I want to create accounts using the name and email fields. In PHP How do I use foreach to loop through the name and email fields so that I can create unique accounts?
My print_r($_POST); array looks like this.
Array
(
[name] => Array
(
[0] => david
[1] => Mark
[2] => cindy
)
[mail] => Array
(
[0] => david#abc.com
[1] => mark#abc.com
[2] => cindy#abc.com
)
[refer_user_id] => 2
[$refer_user_email] => test#abc.com
)
Create a loop with a number of iterations equal to the number of submitted name/email pairs, then use the loop counter to access the values for each user.
for ($i = 0; $i < count($_POST['name']); $i++) {
{
$name = $_POST['name'][$i];
$mail = $_POST['mail'][$i];
// Process the new user
}
go through one of the arrays with a foreach, use the key for the second array.
foreach($_POST['name'] as $key =>$name ){
$mail = $_POST[$key];
}
foreach($_POST['name'] as $key => $val) {
echo $val
}
foreach($_POST['mail'] as $key => $val) {
echo $val
}
Easiest way to loop through those elements. You can reference the other elements with $_POST['refer_user_id']. While this works for the purposes of a foreach, the for loop posted above is more efficient, so I'd recommend using it.
http://php.net/manual/en/control-structures.foreach.php More reading on it here.
You can use array_combine function:
$data = array_combine($_POST['name'],$_POST['mail']);
foreach($data as $name=>$mail){
print $name;
//...
}
See array_combine.
You could also use the JavaScript that's auto-generating the form items to give them a name that would result in linking the php object. i.e.
<div class="user-form">
<input type="text" autocomplete="off" name="user[1][name]" />
<input type="email" autocomplete="off" name="user[1][mail]" />
</div>
<div class="user-form">
<input type="text" autocomplete="off" name="user[2][name]" />
<input type="email" autocomplete="off" name="user[2][mail]" />
</div>
Then you could loop through the pairs with foreach($_POST['user'] as $key=>$value) etc...
I have two arrays. One for input boxes and other for checkbox.
inputbox[] checkbox[]
inputbox[] checkbox[]
.
.
.
.
submit button
When I fill check box1 and fill the value in input box1 and try to submit.
Foreach fails because it pass all the indexes of input boxes but only passes checked checkbox.
foreach(array_combine($checkbox, $inputbox) as $check => $input)
Please tell me what can i do?
if you have a control over the HTML form you can make the form in following manner
<input type="text" name="name[1]" />
<input type="checkbox" name="check[1]" />
<input type="text" name="name[2]" />
<input type="checkbox" name="check[2]" />
<input type="text" name="name[3]" />
<input type="checkbox" name="check[3]" />
<input type="text" name="name[4]" />
<input type="checkbox" name="check[4]" />
in that case you will get the post array in the following manner
Array
(
[name] => Array
(
[1] => Swapnil
[2] =>
[3] => Sarwe
[4] => Swapnil Sarwe
)
[check] => Array
(
[1] => on
[3] => on
)
)
Now you can loop over the name(input box) and then check isset for the isset($_POST['check'][$key]) and set the default value
Iterate over textboxes (which are guaranteed to all be present), then fetch the corresponding checkbox (probably by ID, if you have some kind of ID correspondence between them - which you should).
I have a form, it passes the following values:
image_title,
image_description,
image_file
I wanted to present this form multiple times, so I did the following:
image_title_1,
image_description_1,
image_file_1
image_title_2,
image_description_2,
image_file_2
Multiple times, so I have the fields 1 - 10. I submit the form and print out the contents of the POST array, the only problem is any "image_title_#" after "image_title_1" doesn't exist in the array: but everything else does.
So the array would look something like:
image_title_1 -> "title!"
image_description_1 -> "description!"
image_file_1 -> file
image_description_2 -> "description!"
image_file_2 -> file
image_description_3 -> "description!"
image_file_3 -> file
So to work out what it is I swapped description and title with each other, however title still doesn't display for after 1. I'm not doing ANY processing, I'm literally just printing out the $_POST array before even touching it. This makes no sense, what could be causing it?
To clarify: The problem is "image_title_#" (example: image_title_3) doesn't get passed except for image_title_1, even if I re-arrange the order. I do no processing before outputting.
Edit, the html source is just:
<form method="post" action="">
<input type="text" name="image_title_1"></input>
<input type="text" name="image_description_1"></input>
<input type="text" name="image_file_1"></input>
<input type="text" name="image_title_2"></input>
<input type="text" name="image_description_2"></input>
<input type="text" name="image_file_2"></input>
<input type="text" name="image_title_3"></input>
<input type="text" name="image_description_3"></input>
<input type="text" name="image_file_3"></input>
<input type="submit" name="submit" value="submit"></input>
</form>
A better solution would be converting them to array, try this instead:
<form method="post" action="">
<input type="text" name="image_title[]"></input>
<input type="text" name="image_description[]"></input>
<input type="text" name="image_file[]"></input>
<input type="submit" name="submit" value="submit"></input>
</form>
Now, in your PHP script, you can get their array like this:
print_r($_POST['image_title']);
print_r($_POST['image_description']);
print_r($_POST['image_file']);
.
Suffixing field name with [] converts it to array. The other good thing here is that it has shortened your code too.
Once you have the array, you can loop through them using foreach:
foreach($_POST['image_title'] as $key => $value)
{
// process them any way you want
}
The code works. I just cut and paste your form and do a test submit
Array
(
[image_title_1] => 1
[image_description_1] => 2
[image_file_1] => 3
[image_title_2] => 4
[image_description_2] => 5
[image_file_2] => 6
[image_title_3] => 7
[image_description_3] => 8
[image_file_3] => 9
[submit] => submit
)