This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Simplest method of passing one php variable in a url
I am making a quiz application. At the faculty end, teachers get to type the topic name (with which I will form a vanity URL), and type 3 questions with possible options including answer in one of them. Everytime a teacher creates a quiz, a new table will be created with the questions, options and answers everytime.
My db.php file:
<?php
mysql_connect("localhost","root","");
mysql_select_db("quiz");
?>
My faculty.php file:
<form action="fac_handler.php" method="POST">
Quiz Topic:<br><input type="text" name="topic"><br><br>Enter Questions:<br><br>
<?php
$a="a";
$b="b";
$c="c";
$d="d";
for ($i=1; $i<=3;$i++)
{
?>
Question <?php echo $i?>:<br>
<textarea type="text" name="<?php echo "q".$i;?>"></textarea><br>
<br>
<input type="text" name="<?php echo $a.$i; ?>">
<input type="text" name="<?php echo $b.$i; ?>">
<input type="text" name="<?php echo $c.$i; ?>">
<input type="text" name="<?php echo $d.$i; ?>"> <br><hr>
<?php
}
?>
<br>
<input type="submit" value="Submit">
</form>
My fac_handler.php file:
<?php
require "db.php";
if(isset($_POST['topic']) && !empty($_POST['topic']))
{
$topic=$_POST['topic'];
if(isset($_POST['q1'])&&isset($_POST['a1'])&&isset($_POST['b1'])&&isset($_POST['c1'])&&isset($_POST['d1']))
{
if(!empty($_POST['q1']) && !empty($_POST['a1']) && !empty($_POST['b1']) && !empty($_POST['c1']) && !empty($_POST['d1']))
{
$q1=$_POST['q1'];
$a1a=$_POST['a1'];
$a1b=$_POST['b1'];
$a1c=$_POST['c1'];
$a1d=$_POST['d1'];
}
else
{
echo "Please Set the Questions and the Options!";
die();
}
}
if(isset($_POST['q2'])&&isset($_POST['a2'])&&isset($_POST['b2'])&&isset($_POST['c2'])&&isset($_POST['d2']))
{
if(!empty($_POST['q2']) && !empty($_POST['a2']) && !empty($_POST['b2']) && !empty($_POST['c2']) && !empty($_POST['d2']))
{
$q2=$_POST['q2'];
$a2a=$_POST['a2'];
$a2b=$_POST['b2'];
$a2c=$_POST['c2'];
$a2d=$_POST['d2'];
}
else
{
echo "Please Set the Questions and the Options!";
die();
}
}
if(isset($_POST['q3'])&&isset($_POST['a3'])&&isset($_POST['b3'])&&isset($_POST['c3'])&&isset($_POST['d3']))
{
if(!empty($_POST['q3']) && !empty($_POST['a3']) && !empty($_POST['b3']) && !empty($_POST['c3']) && !empty($_POST['d3']))
{
$q3=$_POST['q3'];
$a3a=$_POST['a3'];
$a3b=$_POST['b3'];
$a3c=$_POST['c3'];
$a3d=$_POST['d3'];
}
else
{
echo "Please Set the Questions and the Options!";
die();
}
}
$ans1=$a1a;
$ans2=$a2a;
$ans3=$a3a;
$qtbname="q_".$topic;
$top=$_SESSION['topic']=serialize($_POST['topic']);
$new="q_".$top;
$create_table="CREATE TABLE $qtbname(id int NOT NULL AUTO_INCREMENT,question VARCHAR(500),answer VARCHAR(30),optiona VARCHAR(30),optionb VARCHAR(30),optionc VARCHAR(30),optiond VARCHAR(30),PRIMARY KEY (id))";
mysql_query($create_table);
$query1="INSERT INTO $qtbname VALUES('','$q1','$ans1','$a1a','$a1b','$a1c','$a1d')";
$query2="INSERT INTO $qtbname VALUES('','$q2','$ans2','$a2a','$a2b','$a2c','$a2d')";
$query3="INSERT INTO $qtbname VALUES('','$q3','$ans3','$a3a','$a3b','$a3c','$a3d')";
$result1=mysql_query($query1);
$result2=mysql_query($query2);
$result3=mysql_query($query3);
header("Location: student.php");
}
else
{
echo "Please Set the Quiz Topic";
}
?>
Now as you can see, after the table gets created dynamically everytime with a table prefix, it gets redirected to student.php file which looks like this.
My student.php file:
<?php
require "db.php";
$query="SELECT * FROM $new";
$result=mysql_query($query);
$n="1";
$i="a";
while($row=mysql_fetch_assoc($result))
{
echo "Question ".$n." : ".$row['question'];
echo "<br>";
$ansr=$row['answer'];
?>
<form action="std_handler.php" method="POST">
<input type="radio" name="<?php echo $i; ?>" value="<?php echo $row['optiona']; ?>"><?php echo $row['optiona']; ?>
<input type="radio" name="<?php echo $i; ?>" value="<?php echo $row['optionb']; ?>"><?php echo $row['optionb']; ?>
<input type="radio" name="<?php echo $i; ?>" value="<?php echo $row['optionc']; ?>"><?php echo $row['optionc']; ?>
<input type="radio" name="<?php echo $i; ?>" value="<?php echo $row['optiond']; ?>"><?php echo $row['optiond']; ?><br>
<?php
++$i;
++$n;
}
?>
<input type="submit" value="Submit">
</form>
The fourth line in this student.php file, the query, $query="SELECT * FROM $new";, I need to access the table which I just created at the faculty end to display the questions and answers in text and radio button format.
Here is my std_handler.php file:
<?php
if(isset($_POST['a']))
{
$ans=$_POST['a'];
echo $ans;
echo "<br>";
}
if(isset($_POST['b']))
{
$ans=$_POST['b'];
echo $ans;
echo "<br>";
}
if(isset($_POST['c']))
{
$ans=$_POST['c'];
echo $ans;
echo "<br>";
}
?>
In the faculty handler file, after the validations, I tried to use session and get the variable, I tried to make it global, I tried serializing it, I couldn't succeed to use and select the dynamic table name in the student.php page.
What I want is, as soon as I get redirected to the student.php page, I want to select the table instantly created earlier, how do I do that? How do I access that variable with which I created the table and executed the query at the faculty end?
I keep getting this error: Notice: Undefined variable: new in C:\xampp\htdocs\quiz\student.php on line 4
Please help me solve this problem. Thank you.
Do you want something like this:
Bad solution, but you can see how to pass a variable
In fac_handler.php:
Change
header("Location: student.php");
to
header("Location: student.php?url=". urlencode($new));
On the student page:
$query="SELECT * FROM " . mysql_real_escape_string($_GET["url"]);
This is a method, but if I where you I would do this on an other way.
How I would do this
Create a ID that you send and resolve that name to the ride database name.
If I had to guess I would say this. When it gives you this error: Notice: Undefined variable: new in C:\xampp\htdocs\quiz\student.php on line 4 it's because it's just that. If we look at the code here:
require "db.php";
$query="SELECT * FROM $new";
$result=mysql_query($query);
$n="1";
$i="a";
this is your student.php ^^^
You are not setting the value of $new.
So you need to actually define the value.
$new = $_GET['x'];
$query="SELECT * FROM $new";
$result=mysql_query($query);
$n="1";
$i="a";
Where x is whatever you are passing as your query value.
or something along those lines.
Okay, I solved this problem, this is what I did.
I removed these two from the fac_handler.php page:
$top=$_SESSION['topic']=serialize($_POST['topic']);
$new="q_".$top;
And few lined below, I changed the header location to:
header("Location: student.php?url=".$qtbname);
And in the student.php page, I changed my query to:
$query="SELECT * FROM " . mysql_real_escape_string($_GET["url"]);
For the last two modifications above, I sincerely thank Lawrence. Also Thunda thanks a lot for giving me that idea to use the GET variable thing too.
deceze and tim.baker (there in the comments): Thanks a lot for your advice too, now that I solved my problem, I will go ahead with normalization as you suggested.
Thanks a lot stackoverflow, was struggling with this for a long time.
Related
I really struggle here and I spent much time on a solution but it seems that I missing something here.
So I have one page that I have 2 values:
$semesterID = $_GET['semesterID'];
$semesterID_old = $semesterID;
I do this as I want to pass 2 values on my next page. I am trying to do that with this:
<form action=ed_semester_v2.php method="post">
<div id="name">
<h2 class="name">Semester ID</h2>
<input class="subjectname" type="text" name="sem_id" id="sem_id" value= "<?php echo $semesterID; ?> " /> <br>
</div>
<input type="hidden" name="semesterID_old" value= "<?php $semesterID_old; ?>"/>
<button type="submit" value="Submit" name="submit">SAVE CHANGES</button>
</form>
The one $semesterID pass fine to the next page but the semesterID_old no. Just to clarify that the semesterid might change by the user and that's ok.
In my next page, I use this code:
if(isset($_POST['submit'])) {
$first_name=$_POST['sem_id'];
$last_name=$_POST['sem_name'];
$semesterID_old= $_POST['semesterID_old'];
if (empty($last_name)) {
echo "Name is empty";
} else {
$query = "UPDATE semesters SET name = '$last_name' where semesterid='$first_name'";
}
if (!mysqli_query($dbconnect, $query)) {
die('An error occurred when submitting your review.');
} else {
// echo "Thanks for your review.";
}
}
echo $first_name;
echo $semesterID_old;
First_name is fine but semesterID_old says that's undefined.
Any help would be much appreciated.
Just change echo semesterID_old; to echo $semesterID_old;
here is my code:
<?php
ob_start();
session_start();
require 'connection.php';
require 'core.inc.php';
?>
<?php
$take_thread_pid_query = #mysql_query(" select pid from threads ");
$row_take_thread_pid = mysql_fetch_array($take_thread_pid_query);
$pid = $row_take_thread_pid['pid'];
while($row_take_thread_pid = #mysql_fetch_array($take_thread_pid_query))
{
?>
<form action="kill_threads.php" method="POST" >
<label> <?php echo "<br/><br/>thread".$row_take_thread_pid['pid']; ?><input type="submit" value = " <?php echo $row_take_thread_pid['pid'];?> " name = " <?php echo
$row_take_thread_pid['pid']; ?> " /> </label>
<?php }?>
</form>
<?php
$t = "4756";//[4756 is on of the pids in my thread table].this is for testing but doesnt works,it cant find any button with this name.
if ( isset($_POST[$t] ) ) echo "im a killed thread..";
?>
The big problem is that im trying to give different names to each button i create,but it seems this is not working because when im trying to see if a button isset['???']
What i have to do...?
For example
thread 1 [button 1]
thread 2 [button 2]
thread 3 [button 3 ]
So if now i click button 1 i want thread1 row deleted from database.
phpmyadmin works like this.
Im so complicated..please help,thanks in advance.
Your code is:
value = " <?php echo $row_take_thread_pid['pid'];?> "
name = " <?php echo $row_take_thread_pid['pid']; ?> "
You are placing spaces before and after the php text so you either have to remove the spaces or code for them
I would suggest you to use another hidden input element within the form instead of doing it with submit form, in this way you can kill all the threads with one if block by passing thread id to it.
while($row_take_thread_pid = #mysql_fetch_array($take_thread_pid_query))
{
?>
<form action="kill_threads.php" method="POST" >
<label> <?php echo "<br/><br/>thread".$row_take_thread_pid['pid']; ?>
<input type="hidden" name="pid" value="<?php echo $row_take_thread_pid['pid'];?>" />
<input type="submit" value = "Delete" name = "delete_thread" /> </label> <?php }?>
</form>
<?php
$t = "4756";//[4756 is on of the pids in my thread table].this is for testing but doesnt works,it cant find any button with this name.
if ( isset($_POST[$t] ) ) echo "im a killed thread..";
?>
Another problem you may come across is using multiple forms without name and id. So add a dynamic number in the form tag like like this
<form name="<?php echo $i; ?>" id="<?php echo $i; ?>" action="kill_threads.php" method="POST" >
Here $i is counter variable or you can use $row_take_thread_pid['pid'] for this purpose.
As the title reveals I got an issue with how to update a checkbox that already has data in my SQL database.
My code looks like following:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name <input type"text" name="inputName" value="<?php echo $hemsida['Namn']; ?>" /> </br>
Commentar <input type"text" name="inputComment" value="<?php echo $hemsida['Comment']; ?>" />
<br/>
</br><input type="checkbox" name="inputAll" value="checked" <?php echo $hemsida['All']; ?>/>Alla
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
<input type="submit" name="submit" value="Redigera">
<?php
if(isset($_POST['submit'])) {
$u = "UPDATE hemsida SET `Namn`='$_POST[inputName]', `Comment`='$_POST[inputComment]', `ALL`='$_POST[inputALL]' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modified";
header("Location: ..//sokh.php");
}
?>
The echo $hemsida['Namn'],['Comment'], and ['All'] just brings up and shows the old data thats in the database, but I do not understand what to do to update the checkbox. I have looked everywhere but I am stuck. Thank you in advance!
If I understand your question correctly, you are looking for a way to have a checkbox be either checked or not checked depending on database info. If so, I would try something like this. At the top of your code where you get your database info, put
if($conditionForCheck){
$inputAll = ' checked="checked"';
}
Then in your form
<input type="checkbox" name="inputAll"<?php echo $inputAll; ?> />
your question is not clear but i think you have a column in your database named "all" ? and perhaps this column can take only 1 value (true or false) !!
then you can test this value in your form, if the value is true : checkbox will be checked, else : checkbox will not be checked :
<input type="checkbox" name="inputAll" checked="<?php if($hemsida['All'] == true) echo checked; ?>" />Alla
dont use value="", use checked instead, then test value of $hemsida['All'] if it's true echo checked else anything to do
for your php code and server side of your application you can just test if checkbox is checked and then you have choice for what do you want to assign to your column in database, for example if checkbox is checked create a variable (for example $value_of_checkbox) and assign a value ("true" for exampel) to this variable, then include this variable in your sql code for update database column :
if (isset($_POST['inputALL'])) {
$value_of_checkbox = true;
}
else {
$value_of_checkbox = false;
}
if(isset($_POST['submit'])) {
$u = "UPDATE hemsida SET `Namn`='$_POST[inputName]', `Comment`='$_POST[inputComment]', `ALL`='$value_of_checkbox' WHERE ID = $_POST[id]";
mysql_query($u) or die(mysql_error());
echo "User has been modified";
header("Location: ..//sokh.php")
note : i change also sql code in this part : ALL='$value_of_checkbox'
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
So, I have a problem with getting data from forms and using it in my codes.
this is choose options page:
<form action="options.php" method="post">
<label>Select number of options:</label>
<select name="options">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select><br><br>
<input type="submit" name="next" value="Next"><br><br>
</form>
and this is options.php page
<?php
// put your code here
$options = $_POST['options'];
if (isset($_POST['submit'])) {
echo $options;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Fill in the following fields:<br><br>
<?php
//loop to prompt the user to enter options' details
for ($i = 1; $i <= $options; $i++) {
$optionName = "option$i";
?>
<?php echo 'Option ' . $i; ?><input type="text" name="<?php echo $optionName; ?>"/><br><br>
<?php
}
?>
<input type="submit" name="submit" value="Next"/>
</form>
Somehow I can't get it working. It keeps giving me an error which is "undefined index". I tried to fix it with isset(). I keep doing something wrong here but I don't know what is it. Will someone please help me or suggest some solutions and ways to get it working. I am new at this and started learning last week.
Put these at the beginning of your code to see what's actually posted (it will also show the get values). These format the Get and Post values nicely. I use them all the time. Once you see what's actually posted you'll be able to see what isn't what you're expecting.
echo("<br><br>Get contents:"); echo("<pre>" . print_r($_GET, 1) . "</pre>");
echo("<br>Post contents:"); echo("<pre>" . print_r($_POST, 1) . "</pre>");
exit;
Your $options = $_POST['options']; should be $options = (isset($_POST['options']) ? $_POST['options'] : "");
You have
<input type="submit" name="next" value="Next"><br><br>
Later you check
if (isset($_POST['submit'])) {
The name you give the input, is the key you want to use when looking up the value.
if (isset($_POST['next'])) {
To start you $_POST['submit'] will never be set because there's nothing posting to that field.
If you want to debug this, let's start by getting the output of EVERYTHING posted. We can do that by doing a print_r($_POST); like the following.
<?php
echo '<pre>';
print_r($_POST);
echo '</pre>'
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Fill in the following fields:<br><br>
<?php
//loop to prompt the user to enter options' details
for ($i = 1; $i <= $options; $i++) {
$optionName = "option$i";
?>
<?php echo 'Option ' . $i; ?><input type="text" name="<?php echo $optionName; ?>"/><br><br>
<?php
}
?>
<input type="submit" name="submit" value="Next"/>
</form>
Then you can see what POST fields are available to you easily and continue coding your project from there.
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.