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.
</div>
$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
foreach($_POST as $key => $value){
#TO DO your operation
}
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!!
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>