kirby
kirby

Reputation: 4041

Form not recognizing post variables

I have a simple html form and some php that input the POST variables into a mysql database. However, I noticed that the form would not input the data when

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

 insert stuff in here
 }

was included. I then removed the if statement above and ran the code. All of the variables were imputed except the ones from the form using POST (ex $_POST['var1']). It seems like the POST variables are not being recognized and I dont know what's wrong.

ALL CODE:

<?php session_start(); ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>

<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.min.js'></script>
<script type='text/javascript' src='http://twitter.github.com/bootstrap/1.4.0/bootstrap-modal.js'></script>
<link rel="stylesheet" href="../css/bootstrap.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/basic.css" type="text/css" media="screen" />
</head>
<body>
<?php include '../css/bar.php'; ?>
<div id='content'>
    <?php include '../nav.php'; 
    ?>



    <?php


    if (isset($_POST['submit'])){
    include '../connect.php';
    $question=mysql_real_escape_string($_POST['question']);
    $detail=mysql_real_escape_string($_POST['detail']);
    $date=date("d M Y");
    $time=time();
    $user=$_SESSION['id'];
    $put=mysql_query("INSERT INTO questions VALUES ('','$question','$detail','$date','$time','$user','subject','0')");

    $result=mysql_query("SELECT * FROM questions WHERE user='$user' AND time='$time'");
        while ($row = mysql_fetch_assoc($result)){

             $q=$row['id'];

            }
    }

    ?>

    <form method='POST' action='question.php?q=<?php echo $q ?>'>   
        <p>Question:</p>
        <p><input type='text' name='question' id='question'  maxlength='200'></p>
        <p>Add some detail (optional):</p>
        <p><textarea id='detail' name='detail' ></textarea></p>
        <p>Tags:</p>
        <p><input type='submit' value='submit' name='submit'></p>
    </form>


</div>
<?php include '../footer.php'; ?>
</body>
</html>

TESTPAGE:

<?php 

 include 'connect.php'; 

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

$hhh=mysql_real_escape_string($_POST['hhh']);
$put=mysql_query("INSERT INTO questions VALUES ('','$hhh','','','','','','')");

}



?>

<form action='test.php' method='post'>

<input type='text' name='hhh'>
<input type='submit' name='submit' value='submit'>
</form>

Upvotes: 0

Views: 2072

Answers (1)

KV Prajapati
KV Prajapati

Reputation: 94653

You have two PHP pages - ask.php and question.php. I (guess)think the ask.php is used to store questions and other details and you want to open a question.php with question id.

ask.php

<?php session_start(); ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>

<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.min.js'></script>
<script type='text/javascript' src='http://twitter.github.com/bootstrap/1.4.0/bootstrap-modal.js'></script>
<link rel="stylesheet" href="../css/bootstrap.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/basic.css" type="text/css" media="screen" />
</head>
<body>
<?php include '../css/bar.php'; ?>
<div id='content'>
    <?php 
     include '../nav.php'; 
     /*--- If submit button is pressed  ---- */
     if (isset($_POST['submit']))
       {
        include '../connect.php';
        $question=mysql_real_escape_string($_POST['question']);
        $detail=mysql_real_escape_string($_POST['detail']);
        $date=date("d M Y");
        $time=time();
        $user=$_SESSION['id'];

        /* SELECT column names you want to use with INSERT statement */
        $put=mysql_query("INSERT INTO questions 
              (`question`,`detail`,`date`,`time`,`user`,`subject`,`name_of_last_col` ) 
                VALUES 
                   ('$question','$detail','$date','$time','$user','subject','0')");
        //for debug purpose 
        if($put)
          {
            echo "Record added";
           }
         else
         {
           echo "Can't add record " . mysql_error();
          }
         }
        /*----- End submit block -----------*/

       /*----List the questions and select it----------*/
        $time=time(); 
        $user=$_SESSION['id'];

        //I think this wont work. Try to remove time comparison from the SELECT statement. 
        $result=mysql_query("SELECT * FROM questions WHERE `user`='$user' AND `time`='$time'");

        //$result=mysql_query("SELECT * FROM questions WHERE `user`='$user'");

        if($result)
         {
            echo "<table>";
            while($row = mysql_fetch_assoc($result))
            {
               echo "<tr>";
               echo "<td>$row[question]</td>";
               echo "<td><a href='question.php?qid=$row[id]'>Show a question</a></td>";
               echo "</tr>";
            }
            echo "</table>";
         }
        else
          {
            echo "No questions!!!";
          }
        ?>

        <form method='POST' action="ask.php">
            <p>Question:</p>
            <p><input type='text' name='question' id='question'  maxlength='200'></p>
            <p>Add some detail (optional):</p>
            <p><textarea id='detail' name='detail' ></textarea></p>
            <p>Tags:</p>
            <p><input type='submit' value='submit' name='submit'></p>
        </form>

question.php should be:

<?php
 $qid=$_GET["qid"];
 echo "$qid is selected...";
?>

You may split code into two PHP pages - one for save records and another to list and select rows.

addquestion.php

<?php 
 session_start(); 

 if(isset($_POST['submit']))
  {
   mysql_connect("localhost","user","password") or die(mysql_error());
   mysql_select_db("your_db_name") or die(mysql_error());

   $question=mysql_real_escape_string($_POST['question']);
   $detail=mysql_real_escape_string($_POST['detail']);
   $date=date("d M Y");
   $time=time();
   $user=$_SESSION['id'];

   $put=mysql_query("INSERT INTO questions 
         (`question`,`detail`,`date`,`time`,`user`,`subject`,`name_of_last_col` ) 
           VALUES 
         ('$question','$detail','$date','$time','$user','subject','0')");
   //for debug purpose 
   if($put)
    {
     echo "Record added";
     }
   else
    {
     echo "Can't add record " . mysql_error();
     }
  }
?>

<form method='post' action="addquestion.php">
 <p>Question:</p>
 <p><input type='text' name='question' id='question'  maxlength='200'></p>
 <p>Add some detail (optional):</p>
 <p><textarea id='detail' name='detail' ></textarea></p>
 <p>Tags:</p>
 <p><input type='submit' value='submit' name='submit'></p>
</form>

questionlist.php

<?php
  session_start(); 
  $user=$_SESSION['id'];
  mysql_connect("localhost","user","password") or die(mysql_error());
  mysql_select_db("your_db_name") or die(mysql_error());  
  $result=mysql_query("SELECT * FROM questions WHERE `user`='$user'");

  if($result)
  {
   echo "<table>";
   while($row = mysql_fetch_assoc($result))
   {
     echo "<tr>";
     echo "<td>$row[question]</td>";
     echo "<td><a href='question.php?qid=$row[id]'>Show a question</a></td>";
     echo "</tr>";
     }
  echo "</table>";
  }
  else
  {
   echo "No questions!!!";
  }
?>

Upvotes: 2

Related Questions