Jump to content

Recommended Posts

Ok..My objective is to provide a simple form....im trying to learn php....where the user can fill out three fields. My theory is to display the form...have them enter the information, when they do this my switch ($_POST['do']) case "check": will display a table with their entered information for them to check over before submitting. This table will also have a submit button witch should activate the case "post": which will submit the data to the database.

 

Problem...it will display the form, but when i press the button it basically refreshes the form with no info, and no table for them to check...and no data in my db....i feel like im close but im not sure if the "switch" function is the right one to use.

 

<?php

/* File: processtest.php*/

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

$title = $_POST['title'];   #10

$description = $_POST['description'];

$event = $_POST['event'];


Print "<table border='7'>
<tr>
<td><b>Title:&nbsp</b></td><td>$title</td>
</tr>                        #20
<tr>
<td><b>Description:&nbsp</b></td><td>$description</td>
</tr>
<tr>
<td><b>Event Type:&nbsp</b></td><td>$event</td>
</tr>
<tr>
<td colspan=2><center><input type='submit' name='do' value='post'>
<input type='hidden' name='do' value='post'></center>
</td>                       #30
</tr>
</table>";

break;

case "post":

$title = $_POST['title'];   #10

$description = $_POST['description'];

$event = $_POST['event'];

$cxn = mysqli_connect($host,$user,$passwd,$dbname)
         or die("Couldn't connect"); #40

$sql = "INSERT INTO test (title, description, event) VALUES ('$title','$description','$event')";

$result = mysqli_query($cxn,$sql)
         or die ("Couldn't execute query.");

include("testdisplay.inc");

break;
                           #50
default:
   include("testdisplay.inc");
  }

?> 

Link to comment
https://forums.phpfreaks.com/topic/141302-my-theory-isnt-working/
Share on other sites

ok heres what i got now..using the ifset function...it displays the form..i enter...and it just sends the info straight to my database...no table for me to check and then submit. any suggestions?

 

<?php

/* File: processtest.php*/

include("caneck.inc");



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

      $title = $_POST['title'];   #10

      $description = $_POST['description'];

      $event = $_POST['event'];


   Print "<table border='7'>
       <tr>
       <td><b>Title:&nbsp</b></td><td>$title</td>
       </tr>                        #20
       <tr>
       <td><b>Description:&nbsp</b></td><td>$description</td>
       </tr>
       <tr>
       <td><b>Event Type:&nbsp</b></td><td>$event</td>
       </tr>
       <tr>
       <td colspan=2><center><form action='process.php method='POST'><input type='submit' name='post' value='post'>
       <input type='hidden' name='do' value='post'></center></form>
       </td>                       #30
       </tr>
       </table>";
   }

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

$title = $_POST['title'];   #10

$description = $_POST['description'];

$event = $_POST['event'];

$cxn = mysqli_connect($host,$user,$passwd,$dbname)
         or die("Couldn't connect"); #40

$sql = "INSERT INTO test (title, description, event) VALUES ('$title','$description','$event')";

$result = mysqli_query($cxn,$sql)
         or die ("Couldn't execute query.");

include("testdisplay.inc");
  }

?> 

 

You are only displaying the form if $_POST['do'] is set in your script. You need to do "if $_POST['do'] is set then insert stuff, else display the form". You'll also want to escape the values from the user prior to using them in your query. In your case you should use the function called mysqli_real_escape_string.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.