Posting and processing data in html checkbox arrays - php

I would like to post data in checkboxes, as like
<?php foreach($data as $foreignid => $id): ?>
<input type="checkbox"
name="photoids[<?php echo $foreignid; ?>]"
value="<?php echo $id;?>"
/>
If I go this way only one key-value pair remains in my $_POST array.
If I leave the photoids[] array empty, (not echoing out the $foreignid) all of the key-value pairs remains in $_POST array, but then i don't have access to the $foreignid variable which that I need in my code.
What is the best workaround for this problem?

Use this:
<?php foreach($data as $foreignid => $id): ?>
<input type="checkbox"
name="photoids_<?php echo $foreignid; ?>"
value="<?php echo $id;?>"
/>
And then parse keys in $_POST array that begin with photoids to obtain foreignid.
Or use this:
<?php foreach($data as $foreignid => $id): ?>
<input type="checkbox"
name="photoids[]"
value="<?php echo $foreignid . '_' . $id;?>"
/>
And then parse values to obtain foreignid and id.
Of course, assuming that underscore '_' will not show up in foreignid or id.
You can parse name or value using:
$src = '123_987';
$arr = explode('_', $src);
$arr[0] will contain 123 and $arr[1] will contain 987.

Maybe something like this:
<?php foreach($data as $foreignid => $id): ?>
<input type="checkbox"
name="photoids[<?php echo $foreignid; ?>]"
value="<?php echo $foreignid."/".$id."/".$data;?>"
/>

Related

How to get 2 value form input array into PHP array

I have an input :
<input type="text" name="input['.$opt_id.']">
and I can get $opt_id value on php side with :
foreach ($_POST['input'] AS $key => $value)
{
$opt_id=$value;
}
but I want to get second value like this :
<input type="text" name=input"['.$opt_id.']['.$lang_id.']">
How can I get $opt_id and $lang_id? I want to insert them on different columns in the database.
Assuming that you don't have 2 entries having the same opt_id and lang_id then you can use a single key instead of 2:
HTML:
<input type="text" name="input[<?php echo "{$opt_id}_{$lang_id}"; ?>]" />
PHP:
foreach ($_POST['input'] as $optIdAndLangId => $value) {
list($opt_id, $lang_id) = explode('_', $optIdAndLangId);
}
Within HTML markup you should insert PHP variables or any other PHP code in such way:
<input type="text" name="input[<?php echo $opt_id; ?>]">
...
<input type="text" name=input"[<?php echo $opt_id; ?>][<?php echo $lang_id; ?>]">
Try Like This
you can process the data with something like this:
<?php
foreach($_POST['input'] as $key => $opt_id){
foreach($opt_id as $ans=>$lang_id){
echo 'option id :'.$ans.' Lang Id : '.$lang_id;
}
}

Coudn't get textbox value

Here, I want to get value of textbox, but I coudn't.
Where am I wrong?
Is it must to use session if i want to get value of textbox?
<input type="submit" id="processorder" name="processorder" class="submit-green" value="Process Order"/>
<?php
foreach ($order_list as $row)
{
?>
<td class="align-center">
<input type="text" name="text[]" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" readonly value="<?php echo $text;?>">
</td>
</tr>
<?php
$i++;
}
?>
if(isset($_POST['processorder']))
{
$txtvalue = $_GET['text[]'];
echo "helo".print_r($txtvalue);
}
this is wrong,
$txtvalue = $_GET['text[]'];
it should be
$txtvalue = $_GET['text'];
^ no [] here
Replace $_GET['text[]'] to $_GET['text']
The field would not be called text[] to PHP. It would just be called text, but would be an array instead of a string.
Try this:
if(isset($_POST['processorder']))
{
$arrayvalue = $_GET['text'];
foreach($arrayvalue as $txtvalue){
echo "helo" . $txtvalue . "<br>";
}
}
As your text input name is text[] with a bracket, it indicates that it is sending as an array, so to get the value you need
if (!empty($_POST["test"][0]) ) {
$test_value = $_POST["test"][0];
}
I am using 0 here since your code only shows one input text, for the server-end will only receive one array value
why you use array name="text[]" if you don't want to get multiple data using "text" than make it name="text"
<input type="text" name="text" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" readonly value="<?php echo $text;?>">
get value using
$_GET['text'] // get value
or
you want to multiple data using same name "Array"(mean using array)
<input type="text" name="text" autocomplete="off" id="txtid_<?php echo $row['id']; ?>" readonly value="<?php echo $text;?>">
get value using
$_GET['text'][0] // 0 is a index value

php foreach loop: parsing values of a form into array

I'm working on a CakePHP project. I want to merge the values of $value into a string/array so I can later merge it with the string "TESTING" then implode the outcome to a single string without spaces!!!
<?php
$merge="TESTING"; ?>
<form method="post" name="payment_form" action="<?php echo $action; ?>">
<?php foreach ($fields as $name => $value): ?>
<input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/>
<?php endforeach; ?>
<p>some text<input type="submit" value="submit"/></p>
</form>
How can this be done? Thanks in advance.
EG: Please I want to keep the code as is. Meaning no for(var i=0;i++...) loops
Example
Let's say the values of the form are 1 2 3 productnr the outcome should be a string 123productnrTESTING no spaces just a string.
Well i wanted to send that data to a bank gateway to be processed. So it goes like this
1)Bank has sent me a string (The one called TESTING)
2)I print my form data (with the above code)
3) I need to send that data + the string provided into an imploded string to the bank gateway. Hope that clarifies some things :)
If I understand your question correctly, you would need to add a hidden field merge as a last one
The form page (View)
<?php
$merge="TESTING";
?>
<form method="post" name="payment_form" action="<?php echo $action; ?>">
<?php foreach ($fields as $name => $value): ?>
<input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/>
<?php endforeach; ?>
<input type="hidden" name="merge" value="<?php echo $merge; ?>
<p>some text<input type="submit" value="submit"/></p>
</form>
The action page (Action Controller)
If you are using CakePHP, you would need to get POST data from the Cake Request object.
$merge = implode($this->request->params);
echo $merge;

PHP: Post to self

I want to write a php page that basically takes an array, $_POST['selection'],
lets a user select one of the keys to delete, and refresh itself with the shorter array.
I saw:
echo '< form method="POST" action="Results.php?selection='.urlencode($myArray).'">;
echo '<input type='submit'></form>';
Would this work?
If not, how should I go about this?
You should be able to tailor this to suit your needs, but with such a general question there are tons of answers.
<?php
if (isset($_POST['selection'])) {
$myArray = $_POST['selection']);
} else {
$myArray = array(
'foo',
'bar',
'more',
'jazz',
);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php foreach ($myArray as $key => $value): ?>
<label for="input<?php echo $key; ?>"><?php echo $value; ?></label>
<input id="input<?php echo $key; ?>" type="checkbox" name="selection[]" checked="checked" value="<?php echo $value; ?>" />
<?php endforeach; ?>
<input type="submit" />
</form>
It would not work, urlencode does not accept an array,
it just return a string, Array
If $myArray is an associative array :-
urlencode(http_build_query($myArray));
Function http_build_query will glue the associate key and value like :-
foo=bar&baz=boom&cow=milk&php=hypertext+processor
And the urlencode will encoded the string properly

PHP - How to submit a form containing input fields that point to different MySQL rows

I am setting up a form using this PHP that loops through all records a user may have:
<?php foreach ($items as $row): ?>
<tr>
<td>
<?php echo form_hidden('id', $row->id); ?>
</td>
<td>
<?php echo '<strong>' . $row->name . '</strong>'; ?>
</td>
<td>
<?php echo form_input('number', $number); ?>
</td>
<td>
<?php echo form_input('registry', $registry); ?>
</td>
<td>
<?php echo form_checkbox('OK', $ok, $ok); ?>
</td>
</tr>
<?php endforeach; ?>
This gives me a form with the following look:
The idea here is that each row belongs to a unique ID/row in the database, and I would like to allow the user to edit all on the same page/form, using a single submit button.
What would be the best way of implementing this?
When this data is submitted, there should be a way of looping through each packet of information (from each user) in my controller. Would this be done via ajax/json?
This does not use codeigntier, but you should be familiar with the general technique before attempting to use CI to shortcut this process. Codeigniter will help you with rendering the form elements, performing validation, escaping your input and performing your query - but it will only help you (do anything) if you understand the basic principles involved. Hope this helps
MARKUP
<form action="/process.php">
<div>
<h2>GORDON</h2>
<input type="text" name="user[1][number]" /> <!-- The number corresponds to the row id -->
<input type="text" name="user[1][registry]" />
<input type="checkbox" name="user[1][ok]" value="1" />
</div>
<div>
<h2>ANDY</h2>
<input type="text" name="user[242][number]" />
<input type="text" name="user[242][registry]" />
<input type="checkbox" name="user[242][ok]" value="1" />
</div>
<div>
<h2>STEWART</h2>
<input type="text" name="user[11][number]" />
<input type="text" name="user[11][registry]" />
<input type="checkbox" name="user[11][ok]" value="1" />
</div>
<input type="submit" />
PHP
$users = $_REQUEST['user'];
foreach ($users as $rowId => $info){
// YOU SHOULD MAKE SURE TO CLEAN YOUR INPUT - THIS IS A GUESS AT WHAT YOUR DATA TYPES MIGHT BE
$id = (int) $rowId;
$number = (int) $info['number'];
$registry = mysql_real_escape_string($info['registry']);
$ok = (int) ($info['ok']);
$q = "UPDATE user SET number = $number, registry = '$registry', ok = $ok WHERE id = $id";
mysql_query($q);
// You may want to check that the above query was sucessful and log any errors etc.
}
There's no need to use ajax mate.
For each put a hidden input with the ID of the row in this format:
<input type="hidden" name="id[<?= $row->id ?>]" value="<?= $row->id ?>" ?>
Do the same for each element in the tr, i.e. name them as
name="number[<?= $row->$id ?>]"
name="registry[<?=$row->$id ?>]"
name="ok[<?=$row->$id ?>]"
and once you post the FORM you can iterate each row with:
foreach ($_POST['id'] as $key => $value) {
echo $_POST['name'][$key];
}
You need to set up input-names as array-names, so you will send the whole form and may iterate over the entries.
e.g.
<?php
echo form_input('userdata[' . $row->id . '][number]', $number);
?>
which would possibly create an
<input name="userdata[1][number]" />
(I don't know where those form-functions came from…)
This will result in an array $_POST['userdata'] which may be iterated via:
foreach($_POST['userdata'] as $userId => $userInputFields)
{
$user = new User($userId);
$user->number = $userInputFields['number'];
// …
}

Categories