PHP: Post to self - php

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

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;
}
}

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;

getting a checkbox array value from POST

i am posting an array of checkboxes. and i cant get it to work. i didnt include the proper syntax in the foreach loop to keep it simple. but it is working. i tested in by trying to do the same thing with a text field instead of a checkbox and it worked with the textfield.
<form method="post">
<?php
foreach{
echo'
<input id="'.$userid.'" value="'.$userid.'" name="invite[]" type="checkbox">
<input type="submit">';
}
?>
</form>
here is the part that is not working. it is echoing 'invite' instead of array.
<?php
if(isset($_POST['invite'])){
$invite = $_POST['invite'];
echo $invite;
}
Your $_POST array contains the invite array, so reading it out as
<?php
if(isset($_POST['invite'])){
$invite = $_POST['invite'];
echo $invite;
}
?>
won't work since it's an array. You have to loop through the array to get all of the values.
<?php
if(isset($_POST['invite'])){
if (is_array($_POST['invite'])) {
foreach($_POST['invite'] as $value){
echo $value;
}
} else {
$value = $_POST['invite'];
echo $value;
}
}
?>
I just used the following code:
<form method="post">
<input id="user1" value="user1" name="invite[]" type="checkbox">
<input id="user2" value="user2" name="invite[]" type="checkbox">
<input type="submit">
</form>
<?php
if(isset($_POST['invite'])){
$invite = $_POST['invite'];
print_r($invite);
}
?>
When I checked both boxes, the output was:
Array ( [0] => user1 [1] => user2 )
I know this doesn't directly answer your question, but it gives you a working example to reference and hopefully helps you solve the problem.
Check out the implode() function as an alternative. This will convert the array into a list. The first param is how you want the items separated. Here I have used a comma with a space after it.
$invite = implode(', ', $_POST['invite']);
echo $invite;
// if you do the input like this
<input id="'.$userid.'" value="'.$userid.'" name="invite['.$userid.']" type="checkbox">
// you can access the value directly like this:
$invite = $_POST['invite'][$userid];
Because your <form> element is inside the foreach loop, you are generating multiple forms. I assume you want multiple checkboxes in one form.
Try this...
<form method="post">
foreach{
<?php echo'
<input id="'.$userid.'" value="'.$userid.'" name="invite[]" type="checkbox">
<input type="submit">';
?>
}
</form>

Insert Multiple data to mysql using a loop ...

so i have this code fragment here..
if($numTF > 0)
{
echo "TRUE-AND-FALSE QUESTIONS: Enter them below followed by their correct answer.";
echo "<br>";?>
<form method="post" action="" name="quizform">
<?php for ($i=1; $i<=$numTF; $i++)
{
echo "Question"." ".$i;
?>`
<p><textarea name='question<?php echo $i; ?>' rows=3 cols=90></textarea></p>
<input type="radio" name="answer<?php echo $i; ?>" value="True"> True
<input type='radio' name="answer<?php echo $i; ?>" value="False"> False<br><br><br>
<?php
}
}
... i am making a quiz maker in php...
the first thing to do is to set up the desired number of questions, so the value entered will go on the $numTF variable. Depending on the entered value, the textarea part will be printed. and there will be different names for each text area. AND THE CODE ABOVE IS WHERE U PRINT THE FORMS AFTER U ENTER THE DESIRED VALUE.
The next thing is to save that in a database. since the name of each textarea will be based on a variable value($i) that is used in a loop (name="answer") , HOW CAN I USE IT IN $_POST??? Like, would i do it like this?? ($_POST['question']).
HOW CAN I SAVE THESE QUESTIONS IN A DATABASE??
PLEASE HELP ME ....
I WOULD BE SO MUCH MUCH MUCH GRATEFUL FOR A LIL HELP.
<?
var_dump($_POST);
?>
<form method="post">
<?
$numTF=4;
if($numTF > 0)
{
echo "TRUE-AND-FALSE QUESTIONS: Enter them below followed by their correct answer.";
echo "<br>";?>
<form method="post" action="" name="quizform">
<?php for ($i=1; $i<=$numTF; $i++)
{
echo "Question"." ".$i;
?>`
<p><textarea name='question[<?php echo $i; ?>]' rows=3 cols=90></textarea></p>
<input type="radio" name="answer[<?php echo $i; ?>]" value="True"> True
<input type='radio' name="answer[<?php echo $i; ?>]" value="False"> False<br><br><br>
<?php
}
}
?>
<input type="submit" name="submit" value="submit"/>
</form>
Use $_POST['question'][1] // To get first question
Use $_POST['answer'][1] // To get first answer
Use loop to get all question and answers
I agree with Sachin as far as using name='question[]'. To answer question a little more as far as storing it in a database. Personally I would use a JSON array.
$store_answers = json_encode($_POST['answer']);
$store_questions = json_encode($_POST['question']);
Then just store $store_string in a TEXT field in your database. Then when you pull it back out of the database you can simple use:
$answers = json_decode($store_answers);
$questions = json_decode($store_questions);
Then you can loop through using a foreach like so:
foreach($questions as $key=>$question) {
echo "Question $key = {$answers[$key]} <br />";
}
This will display the results for each question.

Posting and processing data in html checkbox arrays

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;?>"
/>

Categories