I'm trying to create an elegant form processor that can receive POST OR GET and cycle thru the values and display them. I'd like to make it as simple/elegant as possible, and can't seem to eliminate GET/POST in favor of REQUEST. This code outputs an extra value pair, and I clearly don't understand the relationship of GET/POST to REQUEST. The form includes previous GET information when POST is used (so they seem to overlap) and I'm not sure how to easily NOT include the cookie (I think that's what it is) value. I understand REQUEST is not the best way to do this, but wondering still if there's not a cool way to use it in this fashion. Open to other suggestions.
<h1>
Welcome to the Flexiform! AKA "The Formerator"
</h1>
<p>
This form should process any combination of inputs... this iteration doesn't handle identically-named inputs other than checkboxes. Would like to figure that out.
</p>
<?php
if ($_GET || $_POST)
//if ($_REQUEST) // why can't I use this?... it seems to process the incoming page as GET on firstload...
{
echo ("<h2>Way to '" . $_SERVER['REQUEST_METHOD'] . "' some! </h2>");
foreach($_REQUEST as $submittedName => $submittedValue)
{
if (is_array($submittedValue)) // for checkboxes with more than one selected value
{
$submittedValue = implode(', ', $submittedValue);
}
echo "For the <b>" . $submittedName . " input, you submitted: :</b> " . $submittedValue."</br>";
}
}
else
{
echo ("<h2>Please submit one of the two forms to see some results.</h2>");
}
?>
<hr><hr>
<br>
GET SOME!
<form action= "" method = "get" >
<input type = "text" name = "name" value = "GIcabad" />
<input type = "text" name = "name2" value = "GIcabad2" />
<input type = "text" name = "name3" value = "GIcabad3" />
<input type = "text" name = "name" value = "GIcabadbb" />
<input type = "text" name = "name2" value = "GIcabad2bb" />
<input type = "text" name = "name3" value = "GIcabad3bb" />
orange:<input type = "checkbox" name = "colors[]" value = "orange" checked />
red:<input type = "checkbox" name = "colors[]" value = "red" />
pink:<input type = "checkbox" name = "colors[]" value = "pink" checked />
<input type = "submit" />
</form>
<br>
POST SOME!
<form action = "" method= "post" >
<input type = "text" name = "name" value = "PIcabad" />
<input type = "text" name = "name2" value = "PIcabad2" />
<input type = "text" name = "name3" value = "PIcabad3" />
<input type = "submit" />
</form>
You can loop it once on the main file of your application and store the $_GET and $_POST inside a single variable ( It will act like $_REQUEST but w/o $_COOKIES ). Then you can use it like this
<?php
$input_values = array();
$gpcs = array_merge($_POST, $_GET);
foreach ($gpcs as $key => $value) {
$input_values[$key] = $value;
}
// Then you can use anywhere in your application like this
echo isset($input_values['username'])? $input_values['username']:'';
echo isset($input_values['password'])? $input_values['password']:'';
echo isset($input_values['user_type'])? $input_values['user_type']:'';
?>
Edit: Form and Result
<form action="" method="POST">
<input type="text" name="demo" value="Demo"><br/>
<input type="text" name="demo2" value="Demo 2"><br/>
<input type="text" name="array[]" value="Array 1"><br/>
<input type="text" name="array[]" value="Array 2"><br/>
<input type="text" name="array[]" value="Array 3"><br/>
<input type="text" name="array[]" value="Array 4"><br/>
<input type="submit">
</form>
Array
(
[demo] => Demo
[demo2] => Demo 2
[array] => Array
(
[0] => Array 1
[1] => Array 2
[2] => Array 3
[3] => Array 4
)
)
Related
im fairly new to php and I've been having trouble having the form save the memory for radioboxes, checkboxes & (dropdown) selection list in form so whatever data I put or select will still be there when I submit it.
<select name = "province[]" multiple size = "12">
<?php foreach ($PROVINCES as $key => $value) {?>
<option value = "<?php echo $value ?>"<?php (in_array('$key',$_POST['province'] ) )?'selected':'';?>><?php echo $value ?></option>
<?php }?>
</select>
<br><br>
<label for = "status">Status(Mult Select)<font color = "red">*</font></label>
<input type = "checkbox" name = "status[]" id = "Approved" value= "Approved"> Approved
<input type = "checkbox" name = "status[]" id = "Pending Application" value="Pending Application"> Pending Application
<input type = "checkbox" name = "status[]" id = "Active Service" value="Active Service"> Active Service
<br><br>
<label for = "location">Location<font color = "red">*</font></label>
<input type = "radio" name = "location" id = "Garage" value = "Garage"> Garage
<input type = "radio" name = "location" id= "Attic" value = "Attic"> Attic
<input type = "radio" name = "location" id = "House" value = "House"> House
</fieldset>
My suggestion is to use PHP in_array function to echo out the checked status.
Also, always good to view the status of your POST parameters (helpful figuring out what browser sends to server)
<pre><?php var_dump($_POST); ?></pre>
<input <?php if(in_array('Approved', $_POST['status'])) echo ' checked="checked"'; ?> type = "checkbox" name = "status[]" id = "Approved" value= "Approved"> Approved
<input <?php if(in_array('Pending Application', $_POST['status'])) echo ' checked="checked"'; ?> type = "checkbox" name = "status[]" id = "Pending Application" value="Pending Application"> Pending Application
Hope that will get you on track. Note: there are much better methods available to manage Form elements via PHP though. Look for any frameworks, e.g. Zend Framework.
I have this form html code in update.php. For updating, it's required to link to another page save_seeker.php with mysql update script for it to be executed. Is there any way to execute the script in same page on submitting form so that after query execution it remains on same page?
<form action= "save_seeker.php" method = "post">
Update details !<br><br>
First Name
<input type = "text" name = "fname" value = "<?php echo $disp['fname'];?>"><br><br>
Last Name
<input type = "text" name = "lname" value = "<?php echo $disp['lname'];?>"><br><br>
Contact number
<input type = "text" name = "contact" value = "<?php echo $disp['contact'];?>"><br><br>
Email-id
<input type = "email" name = "email" value = "<?php echo $disp['email'];?>"><br><br>
Address
<input type = "text" name = "address" value = "<?php echo $disp['address'];?>"><br><br>
Experience
<input type = "number" name = "experience" value = "<?php echo $disp['experience'];?> "><br><br>
Qualification
<input type = "text" name = "qualification" value = "<?php echo $disp['qualification'];?>"><br><br>
<input type = "Submit" value = "Update">
</form>
You could do the processing in the same file by adding this into the form action.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<!-- Input fields in here-->
<input type="submit" name="form_submit" value="Submit">
</form>
Now check for the submit action by checking if it is set in the post variable
<?php
if ( isset( $_POST['form_submit'] ) ) {
// Do processing here.
}
?>
You could use the include('page-with-update.php') , call the update function and use the $_SERVER["PHP_SELF"].
I hope helped!!
I am adding text fields to the form dynamically, but now I need to be able to store these values into a mysql database. I am able to do this if there are no dynamically created fields. Here is the code -
<!DOCTYPE HTML>
<html>
<head>
<title> Company Value form </title>
<script language = "javascript">
function addTextField(){
var element = document.createElement("input");
element.setAttribute("name", "i");
element.setAttribute("value", "sample value");
element.setAttribute("size", 30);
var twitter = document.getElementById("twitterusernames");
twitter.appendChild(element);
}
</script>
</head>
<body>
<h1> Enter the values </h1>
<form method = "post" name = "config_form" action = "message.php">
<table border = "1px" >
<tr> <td>
<b> Twitter </b> <br/> <br/>
FeatureId: <input type = "text" name = "FeatureId"> <br/>
Image: <input type = "text" name = "Image"> <br/>
LabelEn: <input type = "text" name = "LabelEn"> <br/>
LabelFr: <input type = "text" name = "LabelFr"> <br/>
URL: <input type = "text" name = "URL"> <br/>
TwitterUserNames: <input type = "text" name = "TwitterUserName" size = "50">
<input type = "button" value = "Add" onclick = "addTextField()"/>
<span id="twitterusernames"> </span>
<br/>
Minimum Count: <input type = "text" name = "MinimumCount"> <br/>
Refresh Count: <input type = "text" name = "RefreshCount"> <br/>
EmptyMessage English: <input type = "text" name = "EmptyMessageEnglish"> <br/>
EmptyMessage French: <input type = "text" name = "EmptyMessageFrench"> <br/>
Widget: <input type = "text" name = "Widget"> <br/>
Email Share Text: <input type = "text" name = "EmailShareText"> <br/>
</td> </tr>
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
and the PHP code:
<?php
include 'database.php';
$Twitter = array(
'FeatureId' => $_POST["FeatureId"],
'Image' => $_POST["Image"],
'LabelEn' => $_POST["LabelEn"],
'LabelFr' => $_POST["LabelFr"],
'URL' => $_POST["URL"],
'TwitterUserName' => $_POST["TwitterUserName"],
'MinimumCount' => $_POST["MinimumCount"],
'RefreshCount' => $_POST["RefreshCount"],
'EmptyMessageEnglish' => $_POST["EmptyMessageEnglish"],
'Widget' => $_POST["Widget"],
'EmailShareText' => $_POST["EmailShareText"]
);
$crud = new database();
$insert = $crud -> insert($Twitter);
if ($insert) {
echo "Success";
}
else {
echo "Values were not inserted into the database";
}
?>
add this element.setAttribute("name", "i[]");
Then in your php handler
$Twitter = array(
'FeatureId' => $_POST["FeatureId"],
'Image' => $_POST["Image"],
'LabelEn' => $_POST["LabelEn"],
'LabelFr' => $_POST["LabelFr"],
'URL' => $_POST["URL"],
'TwitterUserName' => $_POST["TwitterUserName"],
'MinimumCount' => $_POST["MinimumCount"],
'RefreshCount' => $_POST["RefreshCount"],
'EmptyMessageEnglish' => $_POST["EmptyMessageEnglish"],
'Widget' => $_POST["Widget"],
'EmailShareText' => $_POST["EmailShareText"],
'extraFields' => serialize( $_POST["i"] ) // Extra fields
);
$crud = new database();
$insert = $crud -> insert($Twitter);
if ($insert) {
echo "Success";
}
else {
echo "Values were not inserted into the database";
}
?>
to display the data. let's assume that you already have the $extraFields
<?php
$extraFields = unserialize( $extraFields );
foreach($extraFields as $extra ):
?>
<input type="text" name="i[]" value="<?php echo $extra; ?> ">
<?php
endforeach;
?>
Give the dynamic elements names ending in []:
element.setAttribute("name", "i[]");
Then PHP will fill $_POST['i'] with an array of all the values.
According to your script I got idea like you are adding text box after clicking button. So when you are adding multiple textbox you need to make your textbox field name as a array i.e
<input type="text" name="i[]" value="sample data" />
<input type="text" name="i[]" value="sample data" />
<input type="text" name="i[]" value="sample data" />
Modify your addTextField() function
function addTextField(){
var element = document.createElement("input");
element.setAttribute("name", "i[]");
element.setAttribute("value", "sample value");
element.setAttribute("size", 30);
var twitter = document.getElementById("twitterusernames");
twitter.appendChild(element);
}
And in message.php file you can get value through $_POST[i] in array format.
If added fields contain only secondary data, I'd just add another column with text type and just dump there JSON-encoded values of these fields in one chunk.
Basically what I want my program to do is to ask the user how many numbers they want to enter. Once the value is submitted, the program will take the value and create this amount of textboxes. Each textbox will take a number and once submitted, it should take all the numbers (excluding the initial text box) and store it into an array or something so the mean can be calculated.
I've been able to get as far as creating x amount of textboxes but cannot find a way to submit these values.
<html>
<body>
<form action="means.php" method = "get">
Enter sample size: <input type = "number" name = "size" <br>
<input type = "submit">
<?php
if ( isset($_GET["size"] ) )
{
$size = $_GET["size"];
$count = 1;
while ($count <= $size)
{
echo '<br><input type=\"text\" name=\"textbox".$count."\" />';
$count++;
}
}
?>
</form>
</html>
</body>
I assume the problem is that the textboxes created are being echoed so the name "textbox.$count." cannot be used to obtain the numbers?
Any help would be greatly appreciated. Thanks in advance!
Just use PHP's array notation for form field names:
<input type="text" name="textbox[]" />
^^---force array mode
which will produce an array in $_POST['textbox'], one element for each textbox which was submitted with the textbox[] name.
e.g:
<input type="text" name="textbox[]" value="1" />
<input type="text" name="textbox[]" value="foo" />
<input type="text" name="textbox[]" value="banana" />
produces
$_POST = array(
'textbox' => array )
0 => 1,
1 => 'foo',
2 = > 'banana'
)
)
Your problem is that you're using single-quoted strings ('), meaning that
$var = 'foo';
echo '$var' // outputs $, v, a, r
echo "$var" // outputs f, o ,o
In my list there are some name of the input box, it follow the order of 1 2 3....,
<input type= "text" name="text1">
<input type= "text" name="text2">
<input type= "text" name="text3">
that means the post name = text 1 text 2 text 3
However, since the number is not fixed, i don't how many textbox is actually there, how can i get all the name after i posted the form?
Thank you
Use text[] as the name of all your input elements instead of text1, text2 etc. You can then get the values as $_POST['text'], which will be an array. This array will contain as many values as your form contained text boxes.
Update: if you cannot change the HTML (which is unfortunate) you can get the names of the submitted variables by doing this:
$names = array_filter(array_keys($_POST),
function ($k) { return substr($k, 0, 4) == 'text'; });
foreach($_POST as $name => $value) {
echo "$name = $value";
}
should give you the idea
or you can use array text[]:
<input type= "text" name="text[]">
<input type= "text" name="text[]">
<input type= "text" name="text[]">
You can include a hidden field whose value is the names of all such fields.
PUT THIS IN THE HTML FORM
<input type= "text" name="text1" value="1" />
<input type= "text" name="text2" value="2" />
<input type= "text" name="text3" value="3" />
<input type="hidden" name="textContainer" value="text1,text2,text3" />
Then, you can get this variable in PHP like this:
<?php
$textFields = trim( $_POST[ 'textContainer' ] );
$textFields = explode( ',', $textFields );
$fields = array(); // this array will contain all text fields with names text1, text2 as keys
foreach( $textFields as $key => $value ) {
$fields[ $value ] = $_POST[ $value ];
}
/* $fields is now an array like below:
Array (
'text1' => 1,
'text2' => 2,
'text3' => 3
)*/
?>
Let me know if this works.
You can just iterate through the $_POST array itself
Just do a foreach loop however, keep in mind, it is best to know what values are being passed, by doing a catch all maneuver like this, you open yourself up to possible injection and attack.