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.
Related
I have a SQL query statement that will return a particular set of result. Such as ID, Names, Price. I have no problem with that.
However i am trying to add a link within the echo loop and set ID as the value so that i can post it to another page. Would that be possible ?
while ($row = mysql_fetch_array($result)) {
echo
"".$row{'Url'}."<br>";
echo
"Name:".$row{'Name'}."<br>";
echo
"Price: $ ".$row{'Price'}."<br>";
echo
'<div class = "qwerty" data-countdown= '.$row{'Time'}.'></div>';
echo
"Location:".$row{'Location'}."<br>";
echo
"Description:".$row{'Description'}."<br>";
echo ''.$row{'ID'}.'';
echo 'Show Comments
<form id="displayComments" style="display:none" target="jsScript()" method="post">
<input type="hidden" name="run" value=".$row{'ID'}." />
</form>';
You missed quotes. value="'.$row['ID'].'"
echo 'Show Comments
<form id="displayComments" style="display:none" target="jsScript()" method="post">
<input type="hidden" name="run" value="'.$row['ID'].'" />
</form>';
And php array use [ ]
http://ua2.php.net/manual/en/language.types.array.php
I have a form with 640 cards each with a checkbox so the user can mark the ones he owns.
I'm wondering how to write the update SQL statement to take each of the 640 card ids and insert/update into the db.
Each row is created by this code:
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="<?php
echo $row['stickerID'] ?>" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
}
I know I need some kind of for each loop in php. But how do I get the 640 individual checkboxes from the post?
suggest you name them like so
<input type="checkbox" name="stickerid[<?php
echo $row['stickerID'] ?>]" value=" <?php echo $row['stickerStatus'] ?> ">
then you can loop through the $_POST['stickerid'] array
foreach($_POST['stickerid'] as $k=>$v ){
//code
//$k=id
//$v=status
}
I'm kinda rookie at PHP but i'm trying to developing a backoffice with some tables and value-editing options.
By now, i have tables like this:
But i'm kinda bugged with this issue:
The change button is being implemented like this:
for ($i = 0; $i < count($a); $i++) {
?>
<tr>
<td><i onclick="
document.form1.deviceFeatValID.value = <?php echo $a[$i][DEVICE_FEATURE_VALUE_ID] ?>;
document.form1.deviceID.value = <?php echo $a[$i][DEVICE_ID] ?>;
document.form1.deviceClassFeatID.value = <?php echo $a[$i][DEVICE_CLASS_FEATURE_ID] ?>;
document.form1.deviceFeatureVal.value = <?php echo $a[$i][DEVICE_FEATURE_VALUE] ?>;
document.form1.submit();" class="icon-refresh" ></td>
<td><?php echo $a[$i]['DEVICE_FEATURE_VALUE_ID']; ?><td><?php echo $a[$i]['DEVICE_ID']; ?><td><?php echo $a[$i]['DEVICE_CLASS_FEATURE_ID']; ?><td><?php echo $a[$i]['DEVICE_FEATURE_VALUE']; ?>
</tr>
<?php
}
?>
That is, by Javascript, i set the input values of this form, in the same .php:
<form name="form1" method="post" action="deviceFeatureValueFRM.php">
<input type="hidden" name="deviceFeatValID"/>
<input type="hidden" name="deviceID"/>
<input type="hidden" name="deviceClassFeatID"/>
<input type="hidden" name="deviceFeatureVal"/>
<input type="hidden" name="hiddenTypeField"/>
</form>
But...do you consider that this is a good practice? Is there another solution?!
The goal of this code is to detect which line is going about to be edited.
Kind regards,
Sam
Why do this with javascript? Just give the input field a value as such:
<input type="hidden" name="deviceFeatValID" value="<?php echo $a; ?>"/>
Where $a can be any variable you would like.
Well, I would recommend that you had all files separately. So let's say you had one file for PHP. One file for Javascript, and one for HTML. So if you want to use php functions or whatever just need to call them via AJAX.
That way you will have a cleaner code and a bit more secure one.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How to loop through dynamic form inputs and insert into an array
I have a php script and a form. The php script makes an xml file but what i need is for someone to enter a number and that would set that amount of textboxes that would be for someone to write data for that xml file.
So i need it to write <input type="text" name="a #"> however many times the user enters. Also the name needs to be a number but it counts by one ex:<input type="text" name="1"> <input type="text" name="2">... Thanks
<?php
session_start();
if(isset($_POST['quantity']){
// code here to check isnum and min/max
$count = $_POST['quantity'];
for ($i=1; $i<=$count; $i++){
#$s.= "<input type=text name=".$i."><br>";
}
?>
now just echo out $s in your html
This?
<form method="get" action="">
<div><input type="text" name="num_inputs" value="1" placeholder="Number of inputs"/></div>
</form>
<?php $num_inputs = isset($_GET['num_inputs']) ? $_GET['num_inputs'] : 1; ?>
<form method="post" action="">
<?php for ($i = 0; $i < $num_inputs; $i++) : ?>
<div><input type="text" name="inputs[]"/></div>
<?php endfor ?>
</form>
Edit: yes, an array is much better than input_x. Updated my answer.
I think what you want is an array of form fields.
You want something like this:
<?php
$number_of_textboxes = 5; // you'd get this from a $_GET parameter
echo str_repeat('<input type="text" name="mybox[]" />', $number_of_textboxes);
?>
This will print five text boxes:
<input type="text" name="mybox[]" />
Then, when you reference these boxes' values, you do so like thus:
<?php
foreach ($_POST['mybox'] as $i) {
echo $i;
}
?>
That is, by using "mybox[]" as the name of each input field, you create an array of textboxes, which you can then iterate through.
I am creating a new website. For that website I will need to transfer a quantity and amount value from one if condition statement into another if condition statement. Both if statements are accessed by separate submit buttons named "checkamt" & "buy".
I need to transfer the "quantity", "value", and "net" values to and from the checkamt if statement to the buy if statement.
Here's my code:
<form action="index.php" method="post">
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>
<?php
if(isset($_POST[checkamt]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $qun;
echo $val;
echo $total;
}
?>
I think that the problem you're having is that variables don't persist on page change. If you want that, you'll need to use a session. First, you must call session_start before anything, including HTML, is sent to the user. Then, you can use the $_SESSION variable.
<?php
session_start();
?>
<form action="index.php" method="post">
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>
<?php
if(isset($_POST[checkamt]))
{
$_SESSION['qun']=1;
$_SESSION['val']=5000;
$_SESSION['total']=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $_SESSION['qun'];
echo $_SESSION['val'];
echo $_SESSION['total'];
}
?>
Improve your English! Not sure if this is what you want, but if you want to share the values of your variables between the two ifs? You have to declare them at a higher scope than your if:
<?php
$qun = 0;
$val = 0;
$total = 0;
if(isset($_POST[checkamt]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $qun;
echo $val;
echo $total;
}
?>
Not sure if I'm getting the question right, but why not do something like this :
if(isset($_POST[checkamt]) || isset($_POST[buy]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
echo $qun;
echo $val;
echo $total;
}
You need to track your variables between the different forms. You can use SESSION like Xeon06 suggested, or do the following. I'm only showing for $qun:
<?php
if(isset($_POST['checkamt'])) {
$qun=1;
}
if(isset($_POST['buy'])) {
echo $qun;
}
?>
<form action="index.php" method="post">
<input type="hidden" name="qun" value="<?php echo $qun; ?>" />
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>