Jump to content

cant get simple form to work...help


dadamssg

Recommended Posts

so i just made a form that has two fields, a title field and a description field....thats it, with a submit button, im using the switch() function and don't know if thats what i should be doing. ive a file to display the form...tesdisplay.inc, and a file to process the date and send an error message if there is a field left blank or the title is already in use. i want the form to reappear if theres an error, with the messages or send the data to the database if valid and then send the user to google.com...just to see if it works...some help would be AWESOME..im a noob, trying to understand this php enigma

 

the tesdisplay file

<?php

/*File: testdisplay.inc
*Display test form
*/
?>


<html>
<head><title>Test form</title></head>
<body>
<center><h3><b>Test form</b></h3></center>


<?php
     
       if (isset($message))
          {
            echo "<center><font color=red><b>$message</b></font></center>";
          }
?>

<?php
     
       if (isset($messaget))
          {
            echo "<center><font color=red><b>$messaget</b></font></center>";
          }
?>


<center>
<form action="test.php" method="POST">
<Table border='7'>
<tr>
<td><center><b>Title:&nbsp</b></center></td>
<td><input type="test" name="title" size="75" maxsize="75"></td>
</tr>
<tr>
<td valign="top"><br><b>&nbspDescription:&nbsp</b></td>
<td><textarea name='description' cols='57' rows='5'>Type Description here...
</textarea></td>
<tr>
<td colspan='2' align="center">
<input type="submit" name="submit" value="Enter">
<input type="hidden" name="post" value="post">
</td>
</tr>
</table>
</center>
</body>
</html>

 

and the processtest.php

 

<?php

/* File: processtest.php*/

include("caneck.inc");
switch ($_POST['post'])
{
case "post": 

/*check blanks and add errors*/
                                  
if ($_POST['title'] == "")
    
    {
      $blanks[] = "title";
    }
if ($_POST['description'] == "")
   
    {
      $blanks[] = "description";
    }                             

if(isset($blanks))

   {
    $message = "Please enter the: ";

    foreach($blanks as $value)
   {
     $message .= "$value, ";
   }                              
   extract($_POST);
   include("tesdisplay.inc");
   exit();
   }


/*clean data*/


   $title = strip_tags(trim($_POST['title'])); 

   $description = strip_tags(trim($_POST['description'])); 



/*check whether title exists already and set error message*/



$sql = "SELECT title FROM test WHERE title = '$title'";  
$result = mysqli_query($cnx,$sql)
           or die("Couldn't execute select query.");
$num = mysqli_num_rows($result);
if($num > 0)
{
   $messaget = "The title, $title, is already in use.";
include("tesdisplay.inc");
exit();
}
                                
/*add to database*/
else
   {
$sql = "INSERT INTO test (title, description)
               VALUES ('$title','$description')";
$result = mysqli_query($cnx,$sql)
         or die ("Couldn't execute query.");

/*send to google to see if worked*/
                                               
header("Location: www.google.com");
   }
break;

default:
   include("tesdisplay.inc");
}

?>

Link to comment
https://forums.phpfreaks.com/topic/141287-cant-get-simple-form-to-workhelp/
Share on other sites

In your first code box check this line:

<form action="test.php" method="POST">

 

Is test.php the filename of the processing page or is it processtest.php. If I am not reading your question wrong then it should be as follows:

<form action="processtest.php" method="POST">

im actually working on a strategy, but i can't figure out these session variables. im trying to store the user input and then output it in a table them to check before they submit...can you tell me why im getting an error when i try to display the session variables in the table????

 

<?php

/* File: checkpost.php*/

include("caneck.inc");

session_start();

$_SESSION['title'] = $_POST['title'];
$_SESSION['description'] = $_POST['description'];
$_SESSION['event'] = $_POST['event'];



echo "<table border='7'>
       <tr>
       <td><b>Title:&nbsp</b></td><td>$_SESSION['title']</td>
       </tr>                        
       <tr>
       <td><b>Description:&nbsp</b></td><td>$_SESSION['description']</td>
       </tr>
       <tr>
       <td><b>Event Type:&nbsp</b></td><td>$_SESSION['event']</td>
       </tr>
       <tr>
       <td colspan=2><center><form action='submitpost.php' method='POST'><input type='submit' name='post' value='post'>
       </center></form>
       </td>                       
       </tr>
       </table>";
  
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.