Jump to content

[SOLVED] Help with form


tqla

Recommended Posts

Hi. I am trying to write a form that will submit to itself and check to see if the promocode that has been input exists. If it does exist I want is to say "match", if not "no match". I'm new at this and wrote the following code but (of course) it doesn't work. Can someone help me straighten this out?

 

<?php require_once('db/db.php'); ?>
<?php
  session_start();  
  ?>
<form name="form1" method="post" action="<?php echo $PHP_SELF;?>">
  <p>Promotional Code: 
    <input name="promocode" type="text" id="promocode">
</p>
  <p>
    <input type="submit" name="Submit" value="Submit">
</p>
</form>
<?php
if (isset($_POST['submit'])) {
$sql = "SELECT code FROM promocode WHERE code = '$_POST[promocode]'";
$result = mysql_query($sql) or die("Couldn't execute query.");
$num = mysql_num_rows($result);                     
      if ($num == '$_POST[promocode]')  {
  echo "match";
  } else {
  echo "no match";
  }
?>

Link to comment
Share on other sites

Try this code:

 

<?php 
require_once('db/db.php');
session_start();  

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

       $promocode = $_POST['promocode'];

$sql = "SELECT code FROM promocode WHERE code = '$promocode'";
$result = mysql_query($sql) or die(mysql_error()); 
                    
         if (mysql_num_rows($result) > 0)  {
        echo "match";
      } else {
        echo "no match";
      }
}//You were missing this bracket.
?>

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  <p>Promotional Code: 
    <input name="promocode" type="text" id="promocode">
</p>
  <p>
    <input type="submit" name="Submit" value="Submit">
</p>
</form>

 

Here were the issues:

 

-You were using $php_self instead of $_SERVER['PHP_SELF'], so you just had the wrong variable.

-You were comparing the amount of rows returned from the DB to the posted code...and what you really wanted to do was check to see if there was a match in the database.

Link to comment
Share on other sites

got beat dam keyboard.

 

<?php session_start();  
  
require_once('db/db.php');
  
if (isset($_POST['submit'])) {

$promocode=$_POST['promocode'];

$sql = "SELECT code FROM promocode WHERE code = '$promocode";

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

if (mysql_num_rows($result)> 0){  
  
echo "match";

} else {

echo "no match";
  }
        }
?>

<form name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF'] ;?>">
  
<p>Promotional Code: 
    
<input name="promocode" type="text" id="promocode">

</p>
  
<p>
    <input type="submit" name="Submit" value="Submit">

    </p>

</form>

Link to comment
Share on other sites

I've tried the code but it's not exactly doing what it's supposed to. I've tested it with promocode input that should return"match" but the form just reloads itself. I've tried "no match" promocode input but that just reloads too. I'm supposed to see the words "match" or "no match" right?

 

<?php 
require_once('db/db.php');
session_start();  

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

       $promocode = $_POST['promocode'];

$sql = "SELECT code FROM promocode WHERE code = '$promocode'";
$result = mysql_query($sql) or die(mysql_error()); 
                    
         if (mysql_num_rows($result) > 0)  {
        echo "match";
      } else {
        echo "no match";
      }
}
?>

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  <p>Promotional Code: 
    <input name="promocode" type="text" id="promocode">
</p>
  <p>
    <input type="submit" name="Submit" value="Submit">
</p>
</form>

Link to comment
Share on other sites

I want to go a step further with this.

 

I created a form with two text fields that check the input against two tables in the same DB. When I click submit I get either a "match" or no match message. Each field is checked independant of the other. This works fine.

 

What I want to do now is forward the user to another page using the "header("Location: step2.php");" command ONLY if they both "match".

 

I know that I can just separate the form into two pages but I want to combine it into one page.

 

Is this possible?

 

<?php 
require_once('db/db.php');
session_start();  

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

    $promocode = $_POST['promocode'];

$sql = "SELECT code FROM promocode WHERE code = '$promocode'";
$result = mysql_query($sql) or die(mysql_error()); 
                    
         if (mysql_num_rows($result) > 0)  {
        echo "match<BR><BR>";
      } else {
        echo "<font color=\"#FF0000\">The Promotional code that you have entered does not exists or has already been redeemed.</FONT><BR><BR>";
      }
}


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

$accountnumber = $_POST['accountnumber'];

$sql2 = "SELECT accountnumber FROM account WHERE accountnumber = '$accountnumber'";
$result2 = mysql_query($sql2) or die(mysql_error()); 
                    
         if (mysql_num_rows($result2) > 0)  {
        echo "match";
      } else {
        echo "<font color=\"#FF0000\">The Account Number that you have entered does not exists.</FONT>";
      }
}
?>

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  <p>Promotional Code: 
    <input name="promocode" type="text" id="promocode">
</p>
  <p>Account Number: 
    <input name="accountnumber" type="text" id="accountnumber">
  </p>
  <p>
    <input type="reset" name="Reset" value="Reset">
    <input type="submit" name="submit" value="submit">
</p>
</form>

Link to comment
Share on other sites

like this?

 

<?php 
require_once('db/db.php');
session_start();  

$match1 = false;
$match2 = false;

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

    $promocode = $_POST['promocode'];

$sql = "SELECT code FROM promocode WHERE code = '$promocode'";
$result = mysql_query($sql) or die(mysql_error()); 
                    
         if (mysql_num_rows($result) > 0)  {
        $match1 = true;
      } else {
        echo "<font color=\"#FF0000\">The Promotional code that you have entered does not exists or has already been redeemed.</FONT><BR><BR>";
      }
}


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

$accountnumber = $_POST['accountnumber'];

$sql2 = "SELECT accountnumber FROM account WHERE accountnumber = '$accountnumber'";
$result2 = mysql_query($sql2) or die(mysql_error()); 
                    
         if (mysql_num_rows($result2) > 0)  {
        $match2 = true;
      } else {
        echo "<font color=\"#FF0000\">The Account Number that you have entered does not exists.</FONT>";
      }
}

if($match1 && $match2){ //check if both are true
header(); //chuck your header in here
}else{
//one or both failed.
echo "failed";
}
?>

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  <p>Promotional Code: 
    <input name="promocode" type="text" id="promocode">
</p>
  <p>Account Number: 
    <input name="accountnumber" type="text" id="accountnumber">
  </p>
  <p>
    <input type="reset" name="Reset" value="Reset">
    <input type="submit" name="submit" value="submit">
</p>
</form>

Link to comment
Share on other sites

 

<b>Exactly</b> like this ProjectFear!! That's awesome! Thanks.

 

I noticed that when a field does not match the user needs to fill out both fields again.

 

Is there a way to keep the info in the fields so that when a user punches in a wrong code or account number they don't need to start over?

Link to comment
Share on other sites

just set the value to what its name is... haha, weird explanation. example:

 

<?php 
require_once('db/db.php');
session_start();  

$match1 = false;
$match2 = false;

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

    $promocode = $_POST['promocode'];

$sql = "SELECT code FROM promocode WHERE code = '$promocode'";
$result = mysql_query($sql) or die(mysql_error()); 
                    
         if (mysql_num_rows($result) > 0)  {
        $match1 = true;
      } else {
        echo "<font color=\"#FF0000\">The Promotional code that you have entered does not exists or has already been redeemed.</FONT><BR><BR>";
      }
}


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

$accountnumber = $_POST['accountnumber'];

$sql2 = "SELECT accountnumber FROM account WHERE accountnumber = '$accountnumber'";
$result2 = mysql_query($sql2) or die(mysql_error()); 
                    
         if (mysql_num_rows($result2) > 0)  {
        $match2 = true;
      } else {
        echo "<font color=\"#FF0000\">The Account Number that you have entered does not exists.</FONT>";
      }
}

if($match1 && $match2){ //check if both are true
header(); //chuck your header in here
}else{
//one or both failed.
echo "failed";
}
?>

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  <p>Promotional Code: 
    <input name="promocode" type="text" id="promocode" value="<?php echo $_POST['promocode']; ?>">
</p>
  <p>Account Number: 
    <input name="accountnumber" type="text" id="accountnumber">
  </p>
  <p>
    <input type="reset" name="Reset" value="Reset">
    <input type="submit" name="submit" value="submit">
</p>
</form>

 

i did the promocode input box. hope its what your after. :)

Link to comment
Share on other sites

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.