I have a form with a loop inside.
Here is my code:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<?php
for ($i = 1; $i <= 2; $i++) {
echo "Numero ";
echo $i;
echo "<input type='text' name='number2[$i]' id='number2{$i}' />";
}
?>
<input type="submit" name="submitbutton" value="Confirm!">
</form>
<?php
print_r( $_POST );
if(!isset($submitbutton)) {
if (isset($_POST['number2']) != "") {
echo "<b>{$_POST['number2']}</b>, !\n";
$nI = $_POST['number2'];
}
}
?>
The output I get is:
Array ( [number2] => Array ( [1] => 3 [2] => 4 ) [submitbutton] => Confirm! ) Array, !
I would like to know how can I put the number in a session.
For example Session[1]=3, Session[2]=4
I try with array and foreach but I always get error.
Something like this should work for you:
<?php
// Start a PHP Session
session_start();
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<?php
for ($i = 1; $i <= 2; $i++) {
echo "Numero ";
echo $i;
echo "<input type='text' name='number2[$i]' id='number2{$i}' />";
}
?>
<input type="submit" name="submitbutton" value="Confirm!">
</form>
<?php
// If the form was submitted and number2 is an array
if(isset($_POST['submitbutton'])
&& isset($_POST['number2'])
&& is_array($_POST['number2'])) {
// Loop through each posted value and save it to the session
foreach ($_POST['number2'] as $key => $value) {
$_SESSION["number2_{$key}"] = $value;
}
}
echo "number2_1 = " . $_SESSION["number2_1"] . "<br />";
echo "number2_2 = " . $_SESSION["number2_2"] . "<br />";
?>
What error are you getting? Note that isset() merely returns TRUE or FALSE, so isset($_POST['number2']) will never equal the empty string.
Related
}
put returns between paragraphs
for linebreak add 2 spaces at end
italic or bold
indent code by 4 spaces
backtick escapes like _so_
quote by placing > at start of line
to make links (use https whenever possible)
You could have another session that stores the answers as an array, and add to it after each successful post
Something like this could work:
<?php
$totalQuestions = count($ques);
$_SESSION['answers'] = $_SESSION['answers'] ?? [];
// Get current question, default to 1
$currentQuestion = count($_SESSION['answers']) == $totalQuestions ?
$totalQuestions :
$_SESSION['answers'] + 1;
?>
<div class='questionHeader'>
<label>Question <?php echo $currentQuestion ?> of <?php echo $totalQuestions ?></label>
</div>
<br>
<div class='question'>
<?php echo $ques[$currentQuestion-1] ?>
</div>
Answer: <input type='text' id='answerOneSub' name='answerOneSub'>
<button type='submit' value='submit' name='submit'>Submit!</button>
<?php
if (isset($_POST['submit'])) {
$_SESSION['answers'][] = $_POST['answerOneSub'];
echo "<br>" . $_SESSION['answerOneSub'];
}
?>
You can use query param like this:
<?php
$page = 1;
if (isset($_GET["page"])) {
$page = (int)$_GET["page"];
}
$url = strtok($_SERVER["REQUEST_URI"], '?');
echo "<form method='post' action='" . $url . "?page=" . ($page + 1) . "'>";
echo "<div class='questionHeader'><label>Question [$page] of 6</label></div>";
echo "<br>";
echo "<div class='question'>" . $ques[$page - 1] . "</div>";
echo "<br>";
echo "Answer: ";
echo "<input type='text' id='answerOneSub' name='answerOneSub'>";
echo "<button type='submit' value='submit' name='submit'>Submit!</button>";
echo "</form>";
if (isset($_POST['submit'])) {
$_SESSION['answerOneSub'] = $_POST['answerOneSub'];
echo "<br>" . $_SESSION['answerOneSub'];
}
i am trying to do within PHP page.I have form which contains radio button and i want to get their value and place in array. I dont know where i am doing wrong.
code:
$example = array();
$i=0;
while($row = mysql_fetch_array($query) or die(mysql_error())){
$a= $row['A'];
echo '<form method="get" action="?page=".$next."">';
while ($row=mysql_fetch_array($query))
{
echo '<div class="boxed" >';
echo "\t".'<tr><th>'.
$row['question']."<br>".
'</th><th>'."<input type='radio' name= 't[]' value='{$row['A']}'>".$row['A']."<br>".
'</th><th>'."<input type='radio' name='t[]' value='{$row['B']}'>".$row['B']."<br>".
'</th><th>'."<input type='radio' name='t[]' value='{$row['C']}'>".$row['C']."<br>".
'</th><th>'."<input type='radio' name='t[]' value='{$row['D']}'>".$row['D'].'</th>
</tr>';
echo '<input type="hidden" name="page" value="'.$next.'">';
echo '<input type="submit" name="submit"/>';
$i++;
echo '</div>';
echo '</div>';
}
echo '</form>';
if (isset($_GET['submit'])) {
$example[] = $_GET['t'];
echo $example . 'chk';
}
}
replace .$example=$_GET['t']; $example itself a be array. you can process it as you need
if (isset($_GET['submit'])) {
$example[] = $_GET['t'];
echo $example . 'chk';
}
to
if (isset($_GET['submit'])) {
$example = $_GET['t'];
for($i=0;$i<sizeof($example);$i++){
echo $example[$i]."<br>";
}
}
or you can use foreach also
if (isset($_GET['submit'])) {
$example = $_GET['t'];
foreach ($example as $value) {
echo "$value <br>";
}
}
Hello i have one input that generates " text inputs and one submit input" and what is my problem i cant get the value that is written into the generated inputs by user ... here is my code:
<form method="post" action="">
<input type="text" name="generator"/>
<input type="submit" name="generatingsubmit"/>
</form>
<?php
if(isset($_POST['generator'])){
$generator = $_POST['generator'];
echo "<form method='post' action=''>";
for($i = 0; $i < $generator; $i++){
echo "<input type='text' name='" . $i ."'/>";
}
echo "<input type='submit' name='submit'/>";
echo "</form>";
}
echo $_POST[$i];
?>
I made index.php with the code:
<form method="post" action="index.php?action=post">
<input type="text" name="generator"/>
<input type="submit" name="generatingsubmit"/>
</form>
<?php
if (isset($_GET['action']) && $_GET['action']=='post') {
if(isset($_POST['generator'])){
$generator = $_POST['generator'];
echo "<form method='post' action='index.php?action=get_value'>";
for($i = 0; $i < $generator; $i++){
echo "<input type='text' name='somename[]'/><br />";
}
echo "<input type='submit' name='submit' />";
echo "</form>";
}
}
if (isset($_GET['action']) && $_GET['action']=='get_value') {
$somename=$_POST['somename'];
foreach( $somename as $n ) {
print $n;
}
}
?>
All works fine, inputs are generated then values of inputs are received.. All in one index.php file
I submitted a question yesterday but I think it was too long and confusing. Today I shortened everything and submitting an example of my code. The idea behind the code is to parse content, insert the content into a form which will be either echoed in this example or inserted into mysql database. The code works with no errors but show only individual letter and digits instead of full words, please see details below:
<?php
$fullstring = "Name: Steve - Age: 25, Name: Bob - Age: 30, Name: Amanda - Age: 18,"; // Content to be parsed
function get_string_between($string, $start, $end) {
$start = preg_quote($start);
$end = preg_quote($end);
$pattern = "~$start\s*(.*?)$end\s*~";
$match = preg_match_all($pattern, $string, $matches);
if ($match) {
return $matches[1];
}
}
$parsed1 = get_string_between($fullstring, "Name: ", "-");
$parsed2 = get_string_between($fullstring, "Age: ", ",");
////////// The form //////////////////////
echo '<form action="" method="POST">';
for ($i=0; $i < count($parsed1); $i++) {
echo "Name: <input name=\"name\" type=\"text\" value=\"". $parsed1[$i]. "\">","<br>" ;
echo "Age type: <input name=\"age\" type=\"text\" value=\"" . strip_tags($parsed2[$i]) . "\">","<br>" ;
}
echo '<input type="submit" name="submit" value="submit">';
echo '</form>';
if (isset($_POST['submit'])) {
$i = 0;
foreach ($_POST as $value) {
$name = $_POST['name'][$i];
$age = $_POST['age'][$i];
echo $name.'....'.$age.'<br>';
$i++;
}
}
//// mysql_query("INSERT INTO users (name, age) VALUES ('$name', '$age')");
//////////////////////////////////////////////////////////////
?>
When I press submit I get this echoed:
A....1
m....8
a....
Instead of of all the names in the in the form.
I appreciate all the help thank you all.
You can use name[] and age[]
echo '<form action="" method="POST">';
for ($i=0; $i < count($parsed1); $i++) {
echo "Name: <input name=\"name[]\" type=\"text\" value=\"". $parsed1[$i]. "\">","<br>" ;
echo "Age type: <input name=\"age[]\" type=\"text\" value=\"" . strip_tags($parsed2[$i]) . "\">","<br>" ;
}
echo '<input type="submit" name="submit" value="submit">';
echo '</form>';
if (isset($_POST['submit'])) {
foreach($_POST['name'] as $k=>$name){
echo $name.'.... Age:'. $_POST['age'][$k];
}
}
Try this: It is tested:
$fullstring = "Name: Steve - Age: 25, Name: Bob - Age: 30, Name: Amanda - Age: 18,"; // Content to be parsed
function get_string_between($string, $start, $end) {
$start = preg_quote($start);
$end = preg_quote($end);
$pattern = "~$start\s*(.*?)$end\s*~";
$match = preg_match_all($pattern, $string, $matches);
if ($match) {
return $matches[1];
}
}
$parsed1 = get_string_between($fullstring, "Name: ", "-");
$parsed2 = get_string_between($fullstring, "Age: ", ",");
echo '<form action="" method="POST">';
for ($i=0; $i < count($parsed1); $i++) {
echo "Name: <input name=\"name{$i}\" type=\"text\" value=\"". $parsed1[$i]. "\">","<br>" ;
echo "Age type: <input name=\"age{$i}\" type=\"text\" value=\"" . strip_tags($parsed2[$i]) . "\">","<br>" ;
}
echo '<input type="submit" name="submit" value="submit">';
echo '</form>';
if (isset($_POST['submit'])) {
$i = 0;
while ($i < count($parsed1)) {
$name = $_POST['name'.$i];
$age = $_POST['age'.$i];
echo $name.'....'.$age.'<br>';
$i++;
}
}
Your problem is in these lines:
$name = $_POST['name'][$i];
$age = $_POST['age'][$i];
You are getting the character at index $i from each $_POST key. Just delete the [$i] in each of these lines, and your output should become
Ama....18
Ama....18
Note that there is no need to have this in a loop -- you are effectively doing this output twice due to the foreach.
Given the code below when I select a value from my dropdown box [S, M, L] and hit submit I get one of the following outputs:
S is equal to
M is equal to
L is equal to
I would like the output to be along the lines of
S is equal to Small
M is equal to Medium
L is equal to Large
Can something be added to my code to accomplish this? Or do I need to take a different approach?
<form action="?" method="post">
<?php
$size = array();
$size[] = "";
$size[] = "S";
$size[] = "M";
$size[] = "L";
if(isset($_REQUEST['list'])){
echo $size[(int)$_REQUEST['list']]." is equal to "."<br />";
}
echo '<select name="list">'."\n";
$count = 0;
foreach($size as $size){
echo '<option value="'.$count.'">'.$size.'</option>'."\n";
$count++;
}
echo '</select>';
?>
<input type="submit" value="submit" />
</form>
<form action="?" method="post">
Why not use an associative array and then you don't have to mess around with ints or $counts?
<form action="?" method="post">
<?php
$sizes = array(
"S" => "Small",
"M" => "Medium",
"L" => "Large"
);
if(isset($_REQUEST['list'])){
echo "<p>" . $_REQUEST['list'] . " is equal to " . $sizes[$_REQUEST['list']] . "</p>";
}
?>
<select name="list">
<option value="">Choose one</option>
<?php
foreach($sizes as $key => $val){
echo "<option value='$key'>$key - $val</option>\n";
}
?>
</select>
<input type="submit" value="submit" />
</form>
The output will look something like this:
S is equal to Small
+------------+-+
| Choose one |▼|
+------------+-+
| S - Small |
| M - Medium |
| L - Large |
+--------------+
With this solution you can reuse the original static array to populate the post echo. Also try to avoid using \n in your html instead use the semantic <br>.
<form action="?" method="post">
<?php
$size = array(""=>"","Small"=>"S","Medium"=>"M","Large"=>"L");
if(isset($_REQUEST['list'])){
echo $size[$_REQUEST['list']]." is equal to ".$_REQUEST['list']."<br />";
}
echo "<select name='list'>";
foreach($size as $key=>$value){
echo "<option value='$key'>$value</option>";
}
echo '</select>';
?>
<input type="submit" value="submit" />
</form>
You need to make the array $sizes, then foreach ($sizes as $size) for your loop. THen in the echo you need this:
if(isset($_REQUEST['list'])){
switch($size[$_REQUEST['list']])
{
case "S":
$size = "Small";
break;
case "M":
$size = "Medium";
break;
case "L":
$size = "Large";
break;
}
echo $size[(int)$_REQUEST['list']] . " is equal to " . " . $size . "<br />";
}
But really you would be far better using the letters as keys for the array, and the sizes as the values: $sizes['S'] = "Small" then it would be simply in your loop
foreach ($sizes as $key => $size)
{
echo "<option value='" . $key . "'>" . $size . "</option>";
}
and to display:
$_REQUEST['list'] . " is equal to " . " . $sizes['$_REQUEST['list']] . "<br />";
You can get your desired result from rewriting your code using nested arrays for your size array like:
$size = array();
$size[] = "";
$size[] = array("S", "Small");
$size[] = array("M", "Medium");
$size[] = array("L", "Large");
if(isset($_REQUEST['list'])){
echo $size[(int)$_REQUEST['list']][0]." is equal to ". $size[(int)$_REQUEST['list']][1] ."<br />";
}
echo '<select name="list">'."\n";
$count = 0;
foreach($size as $size){
echo '<option value="'.$count.'">'.$size[0].'</option>'."\n";
$count++;
}
echo '</select>';
?>
<input type="submit" value="submit" />
</form>
Im not sure I get your problem right
But why not set the value to Small, Medium & Large while you keep the output S, M, L if you want that.
Then S = Small.