I have some repetitive form field variables and want to put them into variable after POST.
$item_code1 = mysql_sanitize( $_POST['item_code1'] );
$units1 = mysql_sanitize( $_POST['units1'] );
$qty1 = mysql_sanitize( $_POST['qty1'] );
$size1 = mysql_sanitize( $_POST['size1'] );
$make1 = mysql_sanitize( $_POST['make1'] );
$finish1 = mysql_sanitize( $_POST['finish1'] );
$item_code2 = mysql_sanitize( $_POST['item_code2'] );
$units2 = mysql_sanitize( $_POST['units2'] );
$qty2 = mysql_sanitize( $_POST['qty2'] );
...
These set of form variables can be 1 - 10 (or more)
I am passing how many sets of these form variables through a counter
$ctr_i = mysql_sanitize( $_POST['ctr_i'] );
How do i get the variables like above programatically ?
i mean looping through $ctr_i, i want to put all the POST values to PHP variables.
Any ideas?
<?php
foeach($_POST as $key => $val){
$$key = mysql_sanitize($val);
}
?>
You can try something like the below:
$post = $_POST;
$post = array_map('mysql_sanitize', $post);
extract($post);
After this you will have set of the variables.
It would be easier if you have input arrays in your html page like
<input type="text" name="field[]" />
<input type="text" name="field[]" />
<input type="text" name="field[]" />
Then you can collect all the values on the php side by
$field = $_POST['field'];
foreach($field as $a_field)
{
echo $a_field;
}
Related
I have some values in a custom field:
save_post = "1200"
Or it could be, since I need a list eventually:
save_post = "1200, 1460, 1334"
Now when I load the page, I get these values and I set them in an input field, and I also add the current value id from the current page:
$allPosts = $userPosts.", ".$postid;
Where $userPostsis the single value or the value list from the custom field, and $postid is the current page id I want to add.
The result is:
<input type="text" value="1200, 23138, 23138, 23138">
I would always get duplicate values each time I hit the update submit button as the page refreshes itself:
<form id="saveId" action="" method="POST" class="" autocomplete="off">
<input type="text" name="save_post_value" value="<?php echo $userPosts.", ".$postid; ?>">
<button type="submit" class="save_post btn btn-danger">Update</button>
</form>
How can I check if a value is already in the input and if so, don't echo it?
A way would be to have them in an Array and then output in the input field the unique array, not sure if there is a shorter way.
Trying:
$allPosts = array($userPosts.", ".$postid);
$allPosts = array_unique($allPosts);
<input type="text" name="save_post_value" value="<?php foreach ($allPosts as $value) { echo $value; } ?>">
Also:
$allPosts = array($userPosts.", ".$postid);
$allPosts = array_unique($allPosts);
$allPosts = explode(", ", $allPosts);
<input type="text" name="save_post_value" value="<?php echo $allPosts; ?>"
And tried with implode() too:
$allPosts = array($userPosts.", ".$postid);
$allPosts = array_unique($allPosts);
$allPosts = implode(", ", $allPosts);
<input type="text" name="save_post_value" value="<?php echo $allPosts; ?>"
This is very basic example, but I think that this can be useful for your needs:
<?php
// Input data
$userPosts = '19000, 23138, 23138';
$postid = '23138';
// With array
$userPosts = str_replace(' ', '', $userPosts);
if (empty($userPosts)) {
$a = array();
} else {
$a = explode(',', $userPosts);
}
$a = array_unique($a, SORT_STRING);
if (in_array($postid, $a) === false) {
$a[] = $postid;
}
$userPosts = implode(', ', $a);
echo 'Result using array: '.$userPosts.'</br>';
?>
UPDATE:
It is possible to use a function. Check for empty posts is made using empty().
<?php
function getUniquePosts($xposts, $xid) {
$xposts = str_replace(' ', '', $xposts);
if (empty($xposts)) {
$a = array();
} else {
$a = explode(',', $xposts);
}
$a = array_unique($a, SORT_STRING);
if (in_array($xid, $a) === false) {
$a[] = $xid;
}
$xposts = implode(', ', $a);
$xposts = ltrim($xposts, ",");
return $xposts;
}
$userPosts = '19000, 23138, 23138';
$postId = '23138';
echo getUniquePosts($userPosts, $postId).'</br>';
?>
Then when loading form you can try with this:
...
$a = array_unique($a, SORT_STRING);
...
update_user_meta($user_id, 'save_post', getUniquePosts($a, $user_id));
Here is my code on checking duplicate values after submission:
$userPosts = '19000, 23138, 23138';
$postid = '23138';
$pattern = "/(?:^|\W)".$postid."(?:$|\W)/";
if(preg_match($pattern, $userPosts, $matches))
{
print 'There is a duplicate '.rtrim($matches[0] , ",");
}
Basically I reuse Zhorov's variables but on his approach he put it in an array, and then check if that array contains the submitted value, mine is almost the same as his approach but instead of putting it in an array; I use regex to determine is the value existed in the string.
I have tried this simple code to generate an array which will send and a form data in post method. what is the way of receiving this array in desired page? Here is the code:
$serial = 0;foreach ($results as $row) {$serial = $serial + 1;
Html:
<input class="float-lt" type="radio" value=""; ?>" name="question-<?php echo "{$serial}"; ?>[]"/>
<input class="float-lt" type="radio" value=""; ?>" name="question-<?php echo "{$serial}"; ?>[]"/>
$used_serials = $_POST['serials];
foreach( $used_serials AS &$serial ){
$key = 'question-'.$serial;
$wanted_serial_pack = $_POST[$key];
}
OR better use an multidimensional Array structure like:
name="question['serials'][$serial]"
Then you can loop over $_POST['serials]
PHP
$serial = 0;
$inputs = array();
foreach ($results as $row) {
$serial = $serial + 1;
$inputs[] = "<input name=\"question['keys']['{$serial}'][]\"/>";
}
template.php:
echo implode(' ',$inputs);
Submit.php
$results = $_POST['keys];
foreach($results as $serial_array){
var_dump($serial_array);
}
Something like this may help.
So first off, here is the code:
$greens = $_REQUEST['greens'];
$squash = $_REQUEST['squash'];
$tomatoes = $_REQUEST['tomatoes'];
$single = $greens xor $squash xor $tomatoes;
if (isset($greens,$squash,$tomatoes)) {
$array = [$greens,$squash,$tomatoes];
$product = implode(', ',$array);
} else if (isset($greens,$squash)) {
$array = [$greens,$squash];
$product = implode(', ',$array);
} else if (isset($greens,$tomatoes)) {
$array = [$greens,$tomatoes];
$product = implode(', ',$array);
} else if (isset($squash,$tomatoes)) {
$array = [$squash,$tomatoes];
$product = implode(', ',$array);
} else if (isset($single)) {
$product = $single;
} else {
$product = $_REQUEST['vendor_product'];
}
This is part of a php file to submit a vendor registration form. If the vendor selects 'produce' as their type of product, a set of checkbox options appears, and need to select at least one option. Depending on the set of options, the values selected would be collectively submitted into the database in one field. Examples of how they would be viewed in the database are: 'Greens, Squash & Zucchini', 'Greens, Squash & Zucchini, Tomatoes' and 'Greens', etc. where ', ' is inserted if more than one option is selected.
The code above works, but would like to know if there is a way to simplify this, as I will most likely be adding more options for the user to select from. Also, even though there are multiple true results for each condition, can the ternary operator still be used? I am still rather new to understanding that operator.
$names = ['greens', 'squash', 'tomatoes'];
$array = [];
foreach ($names as $name) {
if (isset($_REQUEST[$name])) {
$array[] = $_REQUEST[$name];
}
}
$product = implode(', ',$array);
if (count($array) == 0) {
$product = $_REQUEST['vendor_product'];
}
The best way to simplify this code and make it more flexible at the same time is to change the form itself and use an array.
Instead of
<input type="checkbox" name="green" value="Greens" />
<input type="checkbox" name="squash" value="Squash & Zucchini" />
<input type="checkbox" name="tomatoes" value="Tomatoes" />
You will have
<input type="checkbox" name="produce[]" value="Greens" />
<input type="checkbox" name="produce[]" value="Squash & Zucchini" />
<input type="checkbox" name="produce[]" value="Tomatoes" />
And the PHP code:
if (empty($_REQUEST['produce'])) {
$product = $_REQUEST['vendor_product'];
} else {
$product = implode(', ', $_REQUEST['produce']);
}
or with the ternary operator:
$product = empty($_REQUEST['produce']) ? implode(', ', $_REQUEST['produce']) : $_REQUEST['vendor_product'];
I have a form that sends all the data with jQuery .serialize() In the form are four arrays qty[], etc it send the form data to a sendMail page and I want to get the posted data back out of the arrays.
I have tried:
$qty = $_POST['qty[]'];
foreach($qty as $value)
{
$qtyOut = $value . "<br>";
}
And tried this:
for($q=0;$q<=$qty;$q++)
{
$qtyOut = $q . "<br>";
}
Is this the correct approach?
You have [] within your $_POST variable - these aren't required. You should use:
$qty = $_POST['qty'];
Your code would then be:
$qty = $_POST['qty'];
foreach($qty as $value) {
$qtyOut = $value . "<br>";
}
php automatically detects $_POST and $_GET-arrays so you can juse:
<form method="post">
<input value="user1" name="qty[]" type="checkbox">
<input value="user2" name="qty[]" type="checkbox">
<input type="submit">
</form>
<?php
$qty = $_POST['qty'];
and $qty will by a php-Array. Now you can access it by:
if (is_array($qty))
{
for ($i=0;$i<size($qty);$i++)
{
print ($qty[$i]);
}
}
?>
if you are not sure about the format of the received data structure you can use:
print_r($_POST['qty']);
or even
print_r($_POST);
to see how it is stored.
My version of PHP 4.4.4 throws an error:
Fatal error: Call to undefined function: size()
I changed size to count and then the routine ran correctly.
<?php
$qty = $_POST['qty'];
if (is_array($qty)) {
for ($i=0;$i<count($qty);$i++)
{
print ($qty[$i]);
}
}
?>
try using filter_input() with filters FILTER_SANITIZE_SPECIAL_CHARS and FILTER_REQUIRE_ARRAY as shown
$qty=filter_input(INPUT_POST, 'qty',FILTER_SANITIZE_SPECIAL_CHARS,FILTER_REQUIRE_ARRAY);
then you can iterate through it nicely as
foreach($qty as $key=>$value){
echo $value . "<br>";
}
PHP handles nested arrays nicely
try:
foreach($_POST['qty'] as $qty){
echo $qty
}
I prefer foreach insted of for, because you do not need to heandle the size.
if( isset( $_POST['qty'] ) )
{
$qty = $_POST ['qty'] ;
if( is_array( $qty ) )
{
foreach ( $qty as $key => $value )
{
print( $value );
}
}
}
Hi everybody i need help! I've starting to learn php some week ago.
i have a form that POST some text field for a contact, i need to split the array in some variables to put in a email. this is my code
$field = $_POST['input'];
if ( isset( $field ) === TRUE){
foreach ($field as $key => $value) {
echo '<pre>' , print_r( $value ) , '</pre>'; //to list the array
}
$to = $mail;
$sbj = "thanks to register to $site";
..//some headers
mail($to,sbj,$headers)
}
and this is the form
<form action="upload.php" method="POST">
<input type="text" name="input[]">
<input type="text" name="input[]">
<input type="text" name="input[]">
<input type="submit" value="invia">
</form>
any suggestion for retrive the variables on the array to include on the mail?
#John has given the right procedure . You can use another procedure by using array_push() function eg:
$field = $_POST['input'];
$info = array();
foreach ($field as $key => $value) {
array_push($info,$value);
}
// echo implode(",",$info);
$to = $mail;
$sbj = "thanks to register to $site";
$body = implode(",",$info);
..//some headers
mail($to,sbj,$body,$headers)
You can use the implode() function , which takes an array and a separator and joins the elements of the array together into a string, with each element separated by the separator string you pass it eg:
foreach($field as $key=>$value) {
$input[] = $value;
}
$body = implode("\n",$input); //implode here
$to = $mail;
$sbj = "thanks to register to $site";
..//some headers
mail($to,sbj,$body,$headers)
This would create your list of input each on a separate line in the email.
the easiest, but most ugly way to to this is just using:
mail($to,sbj,print_r($field, true),$headers);
print_r($field, true) will output the value to a variable and not print it out immediately.