Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing a POST variable in another PHP File
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/14331479/simplest-method-of-passing-one-php-variable-in-a-url">Simplest method of passing one php variable in a url</a> </p> </blockquote> <p>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.</p> <p>My db.php file:</p> <pre><code>&lt;?php mysql_connect("localhost","root",""); mysql_select_db("quiz"); ?&gt; </code></pre> <p>My faculty.php file: </p> <pre><code>&lt;form action="fac_handler.php" method="POST"&gt; Quiz Topic:&lt;br&gt;&lt;input type="text" name="topic"&gt;&lt;br&gt;&lt;br&gt;Enter Questions:&lt;br&gt;&lt;br&gt; &lt;?php $a="a"; $b="b"; $c="c"; $d="d"; for ($i=1; $i&lt;=3;$i++) { ?&gt; Question &lt;?php echo $i?&gt;:&lt;br&gt; &lt;textarea type="text" name="&lt;?php echo "q".$i;?&gt;"&gt;&lt;/textarea&gt;&lt;br&gt; &lt;br&gt; &lt;input type="text" name="&lt;?php echo $a.$i; ?&gt;"&gt;&amp;nbsp;&amp;nbsp; &lt;input type="text" name="&lt;?php echo $b.$i; ?&gt;"&gt;&amp;nbsp;&amp;nbsp; &lt;input type="text" name="&lt;?php echo $c.$i; ?&gt;"&gt;&amp;nbsp;&amp;nbsp; &lt;input type="text" name="&lt;?php echo $d.$i; ?&gt;"&gt;&amp;nbsp;&amp;nbsp;&lt;br&gt;&lt;hr&gt; &lt;?php } ?&gt; &lt;br&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt; </code></pre> <p>My fac_handler.php file:</p> <pre><code>&lt;?php require "db.php"; if(isset($_POST['topic']) &amp;&amp; !empty($_POST['topic'])) { $topic=$_POST['topic']; if(isset($_POST['q1'])&amp;&amp;isset($_POST['a1'])&amp;&amp;isset($_POST['b1'])&amp;&amp;isset($_POST['c1'])&amp;&amp;isset($_POST['d1'])) { if(!empty($_POST['q1']) &amp;&amp; !empty($_POST['a1']) &amp;&amp; !empty($_POST['b1']) &amp;&amp; !empty($_POST['c1']) &amp;&amp; !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'])&amp;&amp;isset($_POST['a2'])&amp;&amp;isset($_POST['b2'])&amp;&amp;isset($_POST['c2'])&amp;&amp;isset($_POST['d2'])) { if(!empty($_POST['q2']) &amp;&amp; !empty($_POST['a2']) &amp;&amp; !empty($_POST['b2']) &amp;&amp; !empty($_POST['c2']) &amp;&amp; !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'])&amp;&amp;isset($_POST['a3'])&amp;&amp;isset($_POST['b3'])&amp;&amp;isset($_POST['c3'])&amp;&amp;isset($_POST['d3'])) { if(!empty($_POST['q3']) &amp;&amp; !empty($_POST['a3']) &amp;&amp; !empty($_POST['b3']) &amp;&amp; !empty($_POST['c3']) &amp;&amp; !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"; } ?&gt; </code></pre> <p>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. </p> <p>My student.php file:</p> <pre><code>&lt;?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 "&lt;br&gt;"; $ansr=$row['answer']; ?&gt; &lt;form action="std_handler.php" method="POST"&gt; &lt;input type="radio" name="&lt;?php echo $i; ?&gt;" value="&lt;?php echo $row['optiona']; ?&gt;"&gt;&lt;?php echo $row['optiona']; ?&gt; &lt;input type="radio" name="&lt;?php echo $i; ?&gt;" value="&lt;?php echo $row['optionb']; ?&gt;"&gt;&lt;?php echo $row['optionb']; ?&gt; &lt;input type="radio" name="&lt;?php echo $i; ?&gt;" value="&lt;?php echo $row['optionc']; ?&gt;"&gt;&lt;?php echo $row['optionc']; ?&gt; &lt;input type="radio" name="&lt;?php echo $i; ?&gt;" value="&lt;?php echo $row['optiond']; ?&gt;"&gt;&lt;?php echo $row['optiond']; ?&gt;&lt;br&gt; &lt;?php ++$i; ++$n; } ?&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt; </code></pre> <p>The fourth line in this student.php file, the query, <strong>$query="SELECT * FROM $new";</strong>, 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. </p> <p>Here is my std_handler.php file:</p> <pre><code>&lt;?php if(isset($_POST['a'])) { $ans=$_POST['a']; echo $ans; echo "&lt;br&gt;"; } if(isset($_POST['b'])) { $ans=$_POST['b']; echo $ans; echo "&lt;br&gt;"; } if(isset($_POST['c'])) { $ans=$_POST['c']; echo $ans; echo "&lt;br&gt;"; } ?&gt; </code></pre> <p>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.</p> <p>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?</p> <p>I keep getting this error: <strong>Notice: Undefined variable: new in C:\xampp\htdocs\quiz\student.php on line 4</strong></p> <p>Please help me solve this problem. Thank you. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload