0

how to validate this form. when user submits without selecting the options, he must get an alert.

my code:

echo "<form method='post' id='submit' action='checkresult.php'>";
$sql="SELECT * FROM cquestions where showdate='$today' limit 2";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<p>" . $row['cqtext'] . "</p>";
$sql2="SELECT * FROM canswers where cqid=".$row['cqid'];
$result2=mysql_query($sql2);
while($row2=mysql_fetch_assoc($result2))
{
echo "<input type='radio' name='".$row['cqid']."' value='".$row2['cqans']."' />".$row2['aatext']; }
}
RajkumarLA
  • 13
  • 5

2 Answers2

1

What you are looking for are $_POST Variables. When you submit a form that uses action='checkresult.php', then on the checkresult.php you will be able to use the $_POST command to retrieve the variable values.

test.php page (using what the form outputs):

<form method='post' id='submit' action='checkresult.php'>
<input type='radio' name='the_name' value='the_value' />
<input type="submit">
</form>

checkresult.php:

echo $_POST["the_name"];
// Output = the_value
Midnightly Coder
  • 1,053
  • 1
  • 11
  • 23
  • Nice catch, wonder why it did get down-voted. I down-voted an answer that was removed regarding AJAX, maybe it was retaliation. :) – Midnightly Coder May 16 '13 at 15:29
  • i am displaying questions and answer options from the database. the user should answer those questions and submit back... – RajkumarLA May 17 '13 at 12:22
0

The method you used is correct but syntax is wrong:

<?php
$marks+=$_POST['$cqid']; //Not Correct!
//1st You haven't defined $cqid. Its $qid.
//2nd You can't use a variable inside single quotes.
//PHP will consider it as normal String. But you can use it inside double quotes.
//But remember you can't use array ($row['cqid']) inside double quotes.
?>


This is the correct method:
<?php
while ($row = mysql_fetch_array($result)) {
    //$qid=$row['cqid'];
    //$marks+=$_POST[$qid]; //Correct!
    //But, Not needed You can directly use $row['cqid'] as an index.
    $marks+=$_POST[$row['cqid']];
}
?>


Update: [For debugging]
while ($row = mysql_fetch_array($result)) {
    $marks+=$_POST[$row['cqid']];
    echo $marks.'<br/>';
}
$insert="insert into result(email,marks)values('$email',$marks)";
$insert = mysql_query($insert);
if(!$result) {
    die('Unable to perform insert action. The following error occured: '.  mysql_error());
} else {
   echo 'The following Query: <b>'.$insert.'</b> executed successfully!';
}

Also Check the value of $email I can't see from where you are getting that value from.

And $login_session = $_POST['email']; This line is repeated twice but I am sure this value is always empty, because in test.php you have commented the following line:

echo "<input type='hidden' name='email' value='email' />";

The value attribute: value='email' obviously seems to wrong!

Check all these things, I guess You can carry-on from here now... :) If not, I am still glad to help you...

Update: [For setting limit in query]

SELECT * FROM `cquestions` LIMIT 0,3;
//Will fetch first three records from cquestions.
SELECT * FROM `cquestions` LIMIT 2,3;
//Will fetch 3rd, 4th and 5th records from cquestions.
  • not working! i don't know whether the submitted values are passed from test.php to checkresult.php? when submit button clicks what values are passed to checkresult.php??? hoe to check that? – RajkumarLA May 18 '13 at 11:21
  • `var_dump($_POST);` this will print all the data posted to checkresult.php. If this is empty then nothing is getting posted...! – Elavarasan Muthuvalavan - Lee May 18 '13 at 11:24
  • array(3) { [200]=> string(1) "1" [201]=> string(1) "1" ["submit"]=> string(14) "Submit Answers" } this is what i am getting in checkresult.php – RajkumarLA May 18 '13 at 12:25
  • That means Post values are getting passed... The problem is with your query...! – Elavarasan Muthuvalavan - Lee May 18 '13 at 15:07
  • okay. that means the problem is in the checkresluts.php page... right? – RajkumarLA May 18 '13 at 16:27
  • only once the results were inserted into the database. if a new user comes and submit the answers then the values are passing but insert query seems not working... – RajkumarLA May 19 '13 at 03:16
  • now i want to display questions and multiple answers from database. this code will display all the questions from the database. i want to display only 3 questions daily. every day new set of question should be displayed... how to do it? – RajkumarLA May 19 '13 at 06:06
  • @RajkumarLA : You have to set limit in your query. Check this: [**LINK**](http://php.about.com/od/mysqlcommands/g/Limit_sql.htm) – Elavarasan Muthuvalavan - Lee May 19 '13 at 06:14
  • okay. thanks. there are 100 questions in DB. but i want to display only the 3 questions everyday... how to change questions everyday automatically?! – RajkumarLA May 19 '13 at 07:11
  • @RajkumarLA : Too bad...!!! You have to try on your own... If you are not getting then ask here... – Elavarasan Muthuvalavan - Lee May 19 '13 at 07:36
  • @RajkumarLA : That actually depends upon your code. But refer [**this**](http://stackoverflow.com/questions/1513206/update-multiple-rows-using-limit-in-mysql) to get some idea. – Elavarasan Muthuvalavan - Lee May 22 '13 at 09:02
  • yes its updating every row. but on every page load it will update the next row. i just want to update only one row... they update query must execute only once in a day.. – RajkumarLA May 22 '13 at 11:49
  • Oh... So your query is updating only one row! Just that you should keep a check so that your update query should be executed only once...!!! – Elavarasan Muthuvalavan - Lee May 22 '13 at 11:55
  • update query executing every time page loads. it will check the current date with the showdate in table and display one question (1st row) and update the second row showdate. its correct. but when i refresh the page again, it will update the third row.. that shouldn't happen... – RajkumarLA May 22 '13 at 12:09
  • Option 1: U can make use of Triggers. ; Option 2: U can run a cron job. Option 3: and the best option Ask this a separate question in the same community. :P – Elavarasan Muthuvalavan - Lee May 22 '13 at 12:15