PHP: how to place value from form to array - php

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

Related

posting Php variable without href tag to other file

this is my first file code, where the php code is inside an html form and sending data by post method:
<form name="quiz" action=quizaction.php method="POST">
<?php
$sql = "SELECT * FROM questions WHERE `type` IN
('".implode("','",$fin_element)."')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$index = 0;
while($row = $result->fetch_assoc()) {
echo "<br>";
echo "Q:" . $row["question_name"]. "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.1'/>
<code>".$row["answer1"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.2'/>
<code>".$row["answer2"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.3'/>
<code>".$row["answer3"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.4'/>
<code>".$row["answer4"]."</code>". "<br>";
$index++;
echo $index;
}
} else {
echo "0 results";
}
<INPUT TYPE="SUBMIT" VALUE="Done" >
<INPUT TYPE="RESET" VALUE="Clear all fields of this form">
i want to use the $index variable in other file named quizaction.php file ,how do i ??
Use SESSION
<form name="quiz" action=quizaction.php method="POST">
<?php
session_start();//session starts here
$sql = "SELECT * FROM questions WHERE `type` IN
('".implode("','",$fin_element)."')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$index = 0;
while($row = $result->fetch_assoc()) {
echo "<br>";
echo "Q:" . $row["question_name"]. "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.1'/>
<code>".$row["answer1"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.2'/>
<code>".$row["answer2"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.3'/>
<code>".$row["answer3"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.4'/>
<code>".$row["answer4"]."</code>". "<br>";
$index++;
echo $index;
}
} else {
echo "0 results";
}
// set session value and use $_SESSION['index'] at quizaction.php to find session value
if($index> 0){
$_SESSION['index'] = $index;
}
?>
<INPUT TYPE="SUBMIT" VALUE="Done" >
<INPUT TYPE="RESET" VALUE="Clear all fields of this form">
You can take hidden text-box in your form. then you can get that value in quizaction.php file.
while($row = $result->fetch_assoc()) {
echo "";
echo "Q:" . $row["question_name"]. "";
echo "<input type='radio' name='question".$index."' value='answer1.1'/>
<code>".$row["answer1"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.2'/>
<code>".$row["answer2"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.3'/>
<code>".$row["answer3"]."</code>". "<br>";
echo "<input type='radio' name='question".$index."' value='answer1.4'/>
<code>".$row["answer4"]."</code>". "<br>";
echo "<input type='hidden' name='indexid' value='".$index."'/>
$index++;
echo $index;
}

i need to break the line after a reach of 10 buttons

I need to break the line after a reach of 10 buttons.
Please be specific what are the code to be used for breaking the line.
Here is the code:
<?php
session_start();
require('connection.php');
$result = mysql_query("SELECT * FROM tbCandidates ");
if($result){
}
echo "select Project to Reward for";
$_SESSION['a']="project name-->";
echo "<body><form method='post' action='part1.php' ><input type='hidden' value='abhishek' name='aa'/>";
echo "<table><tr>";
while ($row=mysql_fetch_array($result)){
echo "<td>";
echo $row["project_id"];
echo '<input type="radio" name="n1" value="';echo $row["project_id"]; echo '">'; echo"</input>";
$_SESSION['b']=$row['candidate_name'];
$_SESSION['c']=$row['candidate_position'];
$_SESSION['c']=$row['candidate_nominee'];
echo "</td>";
}
echo "</tr></table><input type='submit'/></form></body>";
If you mean by BREAKING the line is creating another row for your table then try to use the following codes:
<?php
session_start();
require('connection.php');
$result = mysql_query("SELECT * FROM tbCandidates ");
if($result){
}
echo "select Project to Reward for";
$_SESSION['a']="project name-->";
echo "<body><form method='post' action='part1.php' ><input type='hidden' value='abhishek' name='aa'/>";
echo "<table>";
$counter = 0;
while ($row=mysql_fetch_array($result)){
$counter++;
if($counter==1){
echo "<tr>";
}
echo "<td>";
echo $row["project_id"];
echo '<input type="radio" name="n1" value="';echo $row["project_id"]; echo '">'; echo"</input>";
$_SESSION['b']=$row['candidate_name'];
$_SESSION['c']=$row['candidate_position'];
$_SESSION['c']=$row['candidate_nominee'];
echo "</td>";
if($counter==10){
echo "</tr>";
$counter = 0;
}
}
echo "</table><input type='submit'/></form></body>";
Let me know if it works.

Find the variable generated in loop of php

I have this php code:
for($i=0;$i<3;$i++) {
echo 'Question'.$i.'</br>';
echo 'Answer..</br>';
echo '<form action="" method="POST">';
echo '<textarea name="answer"></textarea>';
echo '</br><button name="answer_button'.$i.'"><b>Answer</b></button>';
echo '</form>';
}
Now I want to get the question number for which the answer_button is clicked.
Closest I could get was this:
for($i=0;$i<3;$i++) {
echo 'Question'.$i.'</br>';
echo 'Answer..</br>';
echo '<form action="" method="POST">';
echo '<textarea name="answer"></textarea>';
echo '</br><button name="answer_button"><b>Answer</b></button>';
echo '</form>';
if(isset($_POST['answer_button'])) {
echo $i;
break;
}
}
This gives me Question number but it will not print other questions in loop once the button is clicked.
Is there no solution without using javaScript?
This should work :
for($i=0;$i<3;$i++) {
echo 'Question'.$i.'</br>';
echo 'Answer..</br>';
echo '<form action="" method="POST">';
echo '<textarea name="answer"></textarea>';
echo '</br><button name="answer_button'.$i.'"><b>Answer</b></button>';
echo '</form>';
echo '</div>';
}
for($i=0;$i<3;$i++) {
if(isset($_POST["answer_button".$i])) {
echo $i;
}
}
Hope it helps.
Change the html markup to use an "array notation":
echo '<button name="answer_button['.$i.']" type="submit">Answer</button>';
That will cause php to populate an array when receiving back the form which you can examine:
<?php
// ...
if(isset($_POST['answer_button']) && is_array($_POST['answer_button'])) {
$id = array_shift(array_keys($_POST['answer_button']));
// ...
}
This allows to have multiple such buttons in a single form and detect which one has actually been clicked. It works, because $_POST will contain an array with a single element with key as in $id, which you can easily examine yourself with a var_dump($_POST); or similar.
I think this is what your looking for.
CODE
<?php
echo '<form action="" method="POST">';
for ($i = 0; $i < 3; $i++) {
echo 'Question: ' . $i . '<br>';
echo 'Answer..<br>';
echo "<textarea name='answer[$i]'> </textarea></br>";
echo "</br><button name='answer_button[$i]' value='BtnPushed'> <b>Answer</b></button><br>";
if (! empty($_POST['answer_button'][$i])) echo "Last Answer: {$_POST['answer'][$i]}<br>";
echo '<hr>';
}
echo '</form>';
Results

How to fetch $POST data and put in database

Hi i have a simple form that uploads files and i want to get the id value using $POST and put in the database but my code is incorrect. Here is my code. I just want to ask if im doing the passing and fetching of $POST correctly? thanks
echo "<form action='process.php' method='post' enctype='multipart/form-data' id='uploadfile'>";
echo "<input type='hidden' name='MAX_FILE_SIZE' value='100000' />";
echo "<select name='selectedValue'>";
echo '<option selected="selected">' .'Choose a User'. '</option>';
foreach ($registeredUsers as $key => $value) {
$registered = JFactory::getUser($value);
echo '<option name="id" value="'.$registered->id.'">'.$registered->name.'</option>';
}
echo "</select>";
echo "Choose a file to Upload:";
echo "<input name='uploadedfile' type='file' /> <br/>";
echo '<input type="submit" name="submit" value="Upload">';
echo "</form>";
Here is the process.php file.
if(isset($_POST['selectedValue']))
{
$selectedValue = $_POST['id'];
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query = "INSERT INTO sample_table ('user_id') VALUES ('$_POST[id]')"
$db->setQuery($query);
$result = $db->execute();
}
echo "<form action='process.php' method='post' enctype='multipart/form-data' id='uploadfile'>";
echo "<input type='hidden' name='MAX_FILE_SIZE' value='100000' />";
echo "<select name='id'>";
echo '<option selected="selected">' .'Choose a User'. '</option>';
foreach ($registeredUsers as $key => $value) {
$registered = JFactory::getUser($value);
echo '<option value="'.$registered->id.'">'.$registered->name.'</option>';
}
echo "</select>";
echo "Choose a file to Upload:";
echo "<input name='uploadedfile' type='file' /> <br/>";
echo '<input type="submit" name="submit" value="Upload">';
echo "</form>";
process.php
if(isset($_POST['id']))
{
$selectedValue = $_POST['id'];
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query = "INSERT INTO sample_table ('user_id') VALUES ('$selectedValue')";
$db->setQuery($query);
$result = $db->execute();
}
Okay. So you had some problems, you've set the "name" on the options, which is wrong, you have to set that on the .
Second, you missed a semicolon at the end on $query.
And last, i've chaged to check if the a value has been chosed from the dropdown and to set selectedValue to that, and use that in the query.

validate form in php

I'm trying to validate a form of a test. I get an error in answer.php Basically I want to validate that each question has been answered.
The form:
$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY q_nr";
$result1=mysql_query($sql1);
echo "<form method='post' name='form1' action='answer.php'>";
while($row1 = mysql_fetch_array($result1))
{
$q_nr=$row1['q_nr'];
$q_type=$row1['q_type'];
$question=$row1['question'];
$option1=$row1['option1'];
$option2=$row1['option2'];
$option3=$row1['option3'];
echo "<P><strong>$q_nr $question</strong><BR>";
echo "<BR>";
echo "</p>";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question[$q_nr]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='radio' name='question[$q_nr]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='radio' name='question[$q_nr]' value='C'>$option3<BR>";
} else {
echo ''; }
} else { // else if not <> mr
if($option1!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='C'>$option3<BR>";
} else {
echo ''; }
} //end else if q_type <> mr
echo "<BR>";
echo "</p>";
} //end while row1
echo "<input type='submit' value='Submit' name='Submit'>";
echo "</form>";
answer.php
foreach($_POST['question'] as $key => $ans) {
if ($ans[] = '') {
echo "answer is empty";
}
}
I get the error: Warning: Invalid argument supplied for foreach() in ......
One thing is that you are assigning the answer rather than checking it, use ==
foreach($_POST as $key => $ans) {
if ($ans == '') {
echo "answer is empty";
}
}
and instead of using
name='question[$q_nr]'
I would use for the radio fields
name='question_{$q_nr}'
and for the checkboxes
name='question_{$q_nr}[]'
On answer.php you should be able to do a print_r($_POST) to check what you are getting.
This is probably because your $_POST['question'] is empty. This is what happens when you try to do this with an empty array.
Whereas your HTML says: name='question[$q_nr]'.
Print the values in the array to see what it contains, use print_r.
Edit: $_POST['question'] IS NOT an array! While $_POST IS an array...
Maybe you should try to do something like this:
foreach ($_POST as $key => $value)
Or do it however you want the result to be displayed.

Categories