How would one perform htmlspecialchars on a form that is submitted as an array like this:
<input name="sv_votes[][author]" />
<input name="sv_votes[][author]" />
<input name="sv_votes[][author]" />
This does not seem to do anything:
htmlspecialchars($_POST['sv_votes'])
Loop through array.
foreach($_POST['sv_votes'] as $key => $value){
$_POST['sv_votes'][$key] = htmlspecialchars($value);
}
Related
A page is posting an array to me like this:
<input type="text" name="fields[email_address][value]" value="1" />
<input type="text" name="fields[first_name][value]" value="jim" />
<input type="text" name="fields[zip_code][value]" value="45254" />...
An array.
I can loop through it like this easy enough
foreach ( $_POST['fields'] as $key => $field ) {
echo $key." ".$field['value'] ;
}
Result of above:
first_name jim
email_address 1
address_postal_code 45254
But what I really need to do is reference just the zip (45254) out of the array, maybe like:
echo $_POST['fields']['zip_code']; //or
echo $_POST['fields']['zip_code']['value'];
result: 45254
Is this possible?
Update
<input type="text" name="fields[zip_code][value]" value="45254" />
to be
<input type="text" name="fields[zip_code]" value="45254" />
Edit: I wasn't aware you can't modify the html, that wasn't specified in the original question.
The only thing you can really do is do:
$_POST['fields']['zip_code'] = $_POST['fields']['zip_code']['value'];
However at that point, you might as well just assign $_POST['fields']['zip_code']['value'] to a variable and use that.
If you can't update the html of the form, all you can do is manipulate the data after it's been assigned to the $_POST superglobal like it's any other array
Edit 2: Adding a complete snippet to try:
If you do, what do you get?:
<?php
foreach ( $_POST['fields'] as $key => $field ) {
echo $key." ".$field['value'] ."<br />";
}
echo $_POST['fields']['zip_code']['value'] . "<br />";
$_POST['fields']['zip_code'] = $_POST['fields']['zip_code']['value'];
echo $_POST['fields']['zip_code'];
?>
I just tried that with a simple form of:
<form method="post" action="test.php">
<input type="text" name="fields[email_address][value]" value="1" />
<input type="text" name="fields[first_name][value]" value="jim" />
<input type="text" name="fields[zip_code][value]" value="45254" />
<input type="submit" />
</form>
And it works as expected.
This seems sloppy but it worked:
foreach ( $_POST['fields'] as $key => $field ) {
$$key = $field['value'] ;
}
echo $address_postal_code;
45254
echo $_POST['fields']['zip_code']['value']
returns nothing
I know how to process something like:
<input type="text" name="Textbox_T[]" id="txBox1" />
but I have an unknown number of boxes (they are generated via javascript and are only known to me after they are submitted) that are named like this:
<input type="text" name="Textbox_T1" id="txBox1" />
Textbox_T1
Textbox_T2
Textbox_T3
Textbox_T4
etc
since I cannot do:
$_GET['Textbox_T'.$i]
how do I do it?
You could set the textbox name to an array:
<input type="text" name="textboxes[]" />
<input type="text" name="textboxes[]" />
<input type="text" name="textboxes[]" />
<input type="text" name="textboxes[]" />
and then in the code
if (is_array($_GET["textboxes"])){
foreach ($_GET["textboxes"] AS $value){
echo $value." entered in a textbox.<br />";
}
}
edit: If you can not change the name, you could iterate over ALL
GEt-values:
foreach ($_GET AS $key=>$value){
if (strpos($key, "Textbox") === 0){
echo $value." has been entered in textbox ".$key."<br />";
}
}
Ideally you'd have the javascript add the textareas to submit as an array:
<textarea name="Textbox_T[]" ></textarea>
(I'm assuming you're talking about textareas) because then you just need to loop through that item in PHP once it's been submitted:
foreach($_GET['Textbox_T'] as $text){
//... do something
}
However, if you're stuck with it, you can just loop through your submitted _GET array and attempt to match based on a substring:
$prefix = "Textbox_T";
foreach($_GET as $key=>$value){
if (substr($key,0,strlen($prefix))==$prefix){
//this is one of them! do something
}
}
I have multiple checkbox groups, and once the form is submitted, I want each group of checkboxes that were selected to be added to their own variable.
This is the form:
<form action="" method="get">
<p>apple <input type="checkbox" value="apple" name="fruits[]" /></p>
<p>orange <input type="checkbox" value="orange" name="fruits[]" /></p>
<p>peach <input type="checkbox" value="peach" name="fruits[]" /></p>
<br>
<p>red <input type="checkbox" value="red" name="colors[]" /></p>
<p>green <input type="checkbox" value="green" name="colors[]" /></p>
<p>blue <input type="checkbox" value="blue" name="colors[]" /></p>
<br>
<p>chicken <input type="checkbox" value="chicken" name="meats[]" /></p>
<p>pork <input type="checkbox" value="pork" name="meats[]" /></p>
<p>lamb <input type="checkbox" value="lamb" name="meats[]" /></p>
<button>submit</button>
</form>
And this is my code:
$string = 'fruits,colors,meats';
$str_array = explode(',', $string);
foreach ($str_array as $value) {
if (isset($_GET[$value])) {
$group_name = $_GET[$value];
foreach ($group_name as $group_item) {
$group_string .= ' ' . $group_item;
}
}
}
echo $group_string;
With that code, if I choose for example the first checkbox in each group and hit submit, I will get the following value of $group_string = apple red chicken in one string.
What I get does make sense to me as per the code I wrote, but what I want is for each option group to have its own variable to which its values are asigned, so what I want is to get is the following (assuming I again chose the first option from each group):
$fruits = 'apple';
$colors = 'red';
$meats = 'chicken';
But I don't know how to rewrite it so I get that. Also, the number of options groups is not known upfront, it has to happen dynamically.
Ok, I took the liberty of rewriting part of your php for my convenience but here it is
your new and improved php file
<?php
// assume we know beforehand what we are looking for
$groups = explode(',','fruits,colors,meats');
foreach ($groups as $group) {
if (isset($_GET[$group])) {
$vv = array();
foreach ($_GET[$group] as $item) $vv[] = $item;
$$group = implode(' ',$vv);
}
}
var_dump($fruits,$colors,$meats);
?>
I used a construct in PHP called variable variables. This is actually an almost identical answer as the one #Lohardt gave. I hope this can help you out. If it doesn't then post me a comment
You could do something like:
<input type="checkbox" value="apple" name="groups[fruits][]" />
<input type="checkbox" value="apple" name="groups[colors][]" />
<input type="checkbox" value="apple" name="groups[meats][]" />
Your $_POST will look like this:
Array
(
[groups] => Array
(
[fruits] => Array
(
[0] => apple
)
[colors] => Array
(
[0] => red
)
)
)
And it should be simple to use a foreach loop to get the keys and values.
Edit: then you can assign the value to variables like this:
${$key} = $value;
and use it you would do any variable:
echo $color;
Here i just want to discus about the following:
My HTML From like the fllowing code:
<html>
<head>
<title>My Form</title>
</head>
<body>
<form id="sample" method="post" action="saveData.php">
Courses:
<input type="checkbox" name="check[]" Value="C++" />C++
<input type="checkbox" name="check[]" Value="PHP"/>PHP
<input type="checkbox" name="check[]" Value="MYSQL" />MYSQL
<input type="checkbox" name="check[]" Value=".Net"/>.Net
Gender:
<input type="radio" name="gen[]" Value="male"/>male
<input type="radio" name="gen[]" Value="female"/>Female
</form>
</body>
</html>
And I Want the OutPut Like The Following:
foreach ($_POST as $key => $val) {
$actVal .= "'".strtolower($key)."|".strtolower($val)."',";
$sqlin .= " ".strtolower($key)." VARCHAR(255) , ";
}
But I got The Output like which one clicked in that options:
Like the following:
-----------------------------------------
male
C++
but I need it like the following:
male,female
C++,PHP,MYSQL,.Net
POST will send all values if you mark them as selected using javascript right before submitting. $_POST["check"] is an array. use a foreach and get all values from that array.
As you're loop through post data which is going to be an array I believe that is why only one of their elements are being returned.
You might want to try something like this:
foreach ($_POST as $key => $val) {
if ($key == "check" || $key == "gen") { // If this is an array post field
foreach ($val as $val2) { // We need to loop through again since they're array post fields
$actVal .= "'" . strtolower($val2) . "'";
}
} else {
$actVal .= "'".strtolower($key)."|".strtolower($val)."',";
}
//$sqlin .= " ".strtolower($key)." VARCHAR(255) , "; // Worry about this separately, should be the same process
}
I can't figure out a way around that one. But there is an alternative:
<input type="checkbox" name="check" value="php" />PHP
<input type="hidden" name="checklist" value="php" />
<input type="checkbox" name="check" value="MySQL" />MySQL
<input type="hidden" name="checklist" value="MySQL" />
The idea is to store the list of all the values of a checkbox/radio button, in a hidden input so that you get the list of those values server-side, when the form is submitted.
BTW, why do you need it anyway?
I am trying to take input from a form, add it to an array, and print_r that array to the screen.
My problem is that the input from the form only replaces the first (and only) element in the array.
<form action="" method="POST">
<input type="text" name="text" />
<input type="submit" name="sub"/>
</form>
<?php
$a = array();
if( isset($_REQUEST['text']) && !empty($_REQUEST['text'])){
array_push($a, $_REQUEST['text']);
print_r($a);
}
?>
One theory of mine is that $a keeps getting re-assigned on the first line of PHP code ($a = array();), but I'm not sure how to fix it. I have looked around, but can't find an answer.
You are correct. The array does get reinitialized each time the form is posted. What you'll want to do is have your array as a more persistent data source.
You might consider using a session and the $_SESSION variable.
session_start();
if (!is_array($_SESSION['a'])){
$_SESSION['a'] = array();
}
$_SESSION['a'][] = $_REQUEST['text'];
You might also consider writing this data to a small text file that you could then read at the start of the script.
Another option would be to write the data to a $_COOKIE.
You are mixing client and server execution...
If you want an array of text you should use something like this:
<form action="" method="POST">
<input type="text" name="text[]" />
<input type="text" name="text[]" />
<input type="text" name="text[]" />
<input type="submit" name="sub"/>
</form>
If you want more entry of text being added you should inject more input with javascript
You are not replacing anything, what you're doing is adding the value of $_REQUEST['text'] to the array, which was empty before.