Page 1 of 1

passing values from one php form to another and incrementing

Posted: Thu Apr 01, 2010 11:18 am
by bumdeal2
This php code inserts the values into the database

Code: Select all

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="server2go"; // Database name

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$quesid= $_POST['quesid'];
$question= $_POST['question'];
$answer= $_POST['answer'];
$ranswer = $_POST['ranswer'];
$ranswer1= $_POST['ranswer1'];
$ranswer2= $_POST['ranswer2'];
$qzid= $_POST['qzid'];
$next_id=$_REQUEST['next_id'];


if(($quesid=="")||($quesid==0))
{
$quesid=1;
}
else
{
$quesid=$quesid+1;
}



mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$query = "INSERT INTO questiona (quesid,question,answer,ranswer,ranswer1,ranswer2,qzid)
VALUES
('$quesid','$question','$answer','$ranswer','$ranswer1','$ranswer2','$qzid')";


mysql_query($query) or die('Error1, insert query failed');

$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error2, insert query failed');
$result = @mysql_query($query);

if ($result)
{
header("location: addquizquestion.php?quesid='$quesid'");
exit();
}


?>


This page has the fields ..... that allow the user to enter in the data

Code: Select all

<table width="400" border="0" cellspacing="1" cellpadding="2">

<form method=post name=form action="createquizcheck1.php?quesid='.$quesid.'" onSubmit="return validate_form ( );">

<input type=hidden name=todo value=post>

<tr>
      <td width="100">Question</td>
      <td><input name="question" type="text" id="question"></td>
</tr>
<tr>
      <td width="100">Right answer</td>
      <td><input name="answer" type="text" id="answer"></td>
</tr>
<tr>
      <td width="100">Wrong Answer</td>
      <td><input name="ranswer" type="text" id="ranswer"></td>
</tr>
<tr>
      <td width="100">Wrong Answer</td>
      <td><input name="ranswer1" type="text" id="ranswer1"></td>
</tr>
<tr>
      <td width="100">Wrong Answer</td>
      <td><input name="ranswer2" type="text" id="ranswer2"></td>
</tr>


</select>
</td>
</tr>



<tr>
<td width="100">&nbsp;</td>



<td><input name="add" type="submit" id="add" value="add quiz"></td>
What i wanted to know is how do u make it so that the value quesid automatically passes through the php code and increments each time.Please could you help me.

Re: passing values from one php form to another and incremen

Posted: Thu Apr 01, 2010 1:08 pm
by califdon
You are passing $quesid to addquizquestion.php in the URL, which is the "GET" method, so you just need to retrieve it in that script:

Code: Select all

if(isset($_GET['quesid'])) $quesid = $_GET['quesid'];

Re: passing values from one php form to another and incremen

Posted: Fri Apr 02, 2010 5:41 am
by bumdeal2
I Tried implementing what you said but i am still unable to resolve the issue, i take it it must be the way i am writing the code, could you please show me how to do it properly.

Re: passing values from one php form to another and incremen

Posted: Fri Apr 02, 2010 12:16 pm
by califdon
bumdeal2 wrote:I Tried implementing what you said but i am still unable to resolve the issue, i take it it must be the way i am writing the code, could you please show me how to do it properly.
Show us the code that you wrote and I will try to help you.

Re: passing values from one php form to another and incremen

Posted: Fri Apr 02, 2010 1:00 pm
by bumdeal2

Code: Select all

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="server2go"; // Database name

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");



if(isset($_POST['add']))
{

if(isset($_GET['quesid'])) 
{
$quesid = $_GET['quesid'];


header("location: createquizcheck1.php?quesid=$quesid");
exit();


}
}

?>








<table width="400" border="0" cellspacing="1" cellpadding="2">

<form method=post name=form action="" onSubmit="return validate_form ( );">

<input type=hidden name=todo value=post>

<tr>
      <td width="100">Question</td>
      <td><input name="question" type="text" id="question"></td>
</tr>
<tr>
      <td width="100">Right answer</td>
      <td><input name="answer" type="text" id="answer"></td>
</tr>
<tr>
      <td width="100">Wrong Answer</td>
      <td><input name="ranswer" type="text" id="ranswer"></td>
</tr>
<tr>
      <td width="100">Wrong Answer</td>
      <td><input name="ranswer1" type="text" id="ranswer1"></td>
</tr>
<tr>
      <td width="100">Wrong Answer</td>
      <td><input name="ranswer2" type="text" id="ranswer2"></td>
</tr>


</select>
</td>
</tr>


<tr>
<td width="100">&nbsp;</td>



<td><input name="add" type="submit" id="add" value="addquiz"></td>





and the other page is

Code: Select all

<?php


$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="server2go"; // Database name

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$question= $_POST['question'];
$answer= $_POST['answer'];
$ranswer = $_POST['ranswer'];
$ranswer1= $_POST['ranswer1'];
$ranswer2= $_POST['ranswer2'];
$qzid= $_POST['qzid'];

if(isset($_GET['quesid'])) 
{
$quesid = $_GET['quesid'];



if((quesid==0)||(quesid==""))
{
$quesid=1;
}
else
{
$quesid=$quesid+1;
}



mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$query = "INSERT INTO questiona (quesid,question,answer,ranswer,ranswer1,ranswer2,qzid)
VALUES
('$quesid','$question','$answer','$ranswer','$ranswer1','$ranswer2','$qzid')";


mysql_query($query) or die('Error1, insert query failed');

$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error2, insert query failed');
$result = @mysql_query($query);

if ($result)
{
header("location: addquizquestion.php?quesid=$quesid");
exit();


}
}


?>

Please dont be harsh im a newbie willing to learn:-)

Re: passing values from one php form to another and incremen

Posted: Fri Apr 02, 2010 1:16 pm
by learnerabn
bumdeal2 wrote:This php code inserts the values into the database

Code: Select all

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="server2go"; // Database name

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$quesid= $_POST['quesid'];
$question= $_POST['question'];
$answer= $_POST['answer'];
$ranswer = $_POST['ranswer'];
$ranswer1= $_POST['ranswer1'];
$ranswer2= $_POST['ranswer2'];
$qzid= $_POST['qzid'];
$next_id=$_REQUEST['next_id'];


if(($quesid=="")||($quesid==0))
{
$quesid=1;
}
else
{
$quesid=$quesid+1;
}



mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$query = "INSERT INTO questiona (quesid,question,answer,ranswer,ranswer1,ranswer2,qzid)
VALUES
('$quesid','$question','$answer','$ranswer','$ranswer1','$ranswer2','$qzid')";


mysql_query($query) or die('Error1, insert query failed');

$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error2, insert query failed');
$result = @mysql_query($query);

if ($result)
{
header("location: addquizquestion.php?quesid='$quesid'");
exit();
}


?>


This page has the fields ..... that allow the user to enter in the data

Code: Select all

<table width="400" border="0" cellspacing="1" cellpadding="2">

<form method=post name=form action="createquizcheck1.php?quesid='.$quesid.'" onSubmit="return validate_form ( );">

<input type=hidden name=todo value=post>

<tr>
      <td width="100">Question</td>
      <td><input name="question" type="text" id="question"></td>
</tr>
<tr>
      <td width="100">Right answer</td>
      <td><input name="answer" type="text" id="answer"></td>
</tr>
<tr>
      <td width="100">Wrong Answer</td>
      <td><input name="ranswer" type="text" id="ranswer"></td>
</tr>
<tr>
      <td width="100">Wrong Answer</td>
      <td><input name="ranswer1" type="text" id="ranswer1"></td>
</tr>
<tr>
      <td width="100">Wrong Answer</td>
      <td><input name="ranswer2" type="text" id="ranswer2"></td>
</tr>


</select>
</td>
</tr>



<tr>
<td width="100">&nbsp;</td>



<td><input name="add" type="submit" id="add" value="add quiz"></td>
What i wanted to know is how do u make it so that the value quesid automatically passes through the php code and increments each time.Please could you help me.
is this ur original code?
if is it so...then u cant use php variable($quesid.) in html codes directly u have to include<?php echo"$quesid.";?> in the html code...
i think this may be the problem..

Re: passing values from one php form to another and incremen

Posted: Fri Apr 02, 2010 3:08 pm
by califdon
So you have 2 PHP scripts, the first of which displays a form for the user to fill in a question and 4 answers, the other of which receives the form data from the first script and inserts a new record in a database, then calls the first script again, right?

The first thing I see is that in the first script you are testing for whether a $_POST variable named "add" has been set. But since your calling script (the second one) doesn't have any form at all, much less an input named "add", there will never be such a variable set, so none of that code will ever run.

Then, in your second script you are referring to a variable that you don't have in the form in the first script: "qzid". That won't work, of course. Then you are also referring to another variable, "quesid", in the second script that you don't have in the first script. You can only recover $_POST variables (from a form) or $_GET variables (from the URL, like "?id=123" will be the variable $_GET['id']) if they are in the form or the URL. I also see that you have a hidden input named "todo" that you totally ignore in the second script.

The easiest way to do this is to create a hidden input field in the form, with the name 'quesid'. But of course, you will have to specify the value of that input field, since it will be hidden and the user will not enter the value. So your first script needs to look more like this:

Code: Select all

...
if(isset($_GET['quesid'])) {
   $quesid=1;
} else {
   $quesid=$_GET['quesid'];
}
...
<input type='hidden" name="quesid" value="<?php echo $quesid;?>">
...
and in the second script, you have to get the "quesid" variable, along with the other form variables, then increment it by 1 and send it back in the URL when you send the header to call the first script again:

Code: Select all

...
$quesid = $_POST['quesid'];
$question = $_POST['question'];
$answer = $_POST['answer'];
$ranswer = $_POST['ranswer'];
$ranswer1 = $_POST['ranswer1'];
$ranswer2 = $_POST['ranswer2'];
...
// there will be no $_GET variables, so remove those lines
...
$query = "INSERT INTO questiona (quesid,question,answer,ranswer,ranswer1,ranswer2)
VALUES
('$quesid','$question','$answer','$ranswer','$ranswer1','$ranswer2')";
...
$quesid++;  // increments $quesid for the next question
...
header("location: addquizquestion.php?quesid=$quesid");
As you learn PHP programming, you will have to pay attention to several things:
  1. Basic program flow logic
  2. PHP syntax
  3. Understand what you are doing, not just follow what you find in a forum or somebody else's script
If you will work on these things, you will progress to the point where you can come back here and explain to other beginners what they need to do.

Good luck.

Re: passing values from one php form to another and incremen

Posted: Fri Apr 02, 2010 7:13 pm
by bumdeal2
I truly appreciate all the help you've given me as well as great advice, Thank you for taking the time for responding to my questions, Much appreciated.