tqla Posted June 28, 2007 Share Posted June 28, 2007 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"; } ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 28, 2007 Share Posted June 28, 2007 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. Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 28, 2007 Share Posted June 28, 2007 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> Quote Link to comment Share on other sites More sharing options...
tqla Posted June 28, 2007 Author Share Posted June 28, 2007 Hi pocobueno1388 and redarrow. Thank you both! You two have helped me before and I really appreciate it. I'm doing my best here and learning everyday. Quote Link to comment Share on other sites More sharing options...
tqla Posted June 28, 2007 Author Share Posted June 28, 2007 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> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 28, 2007 Share Posted June 28, 2007 It's because your submit button name has a capital s. Change this line: <input type="submit" name="Submit" value="Submit"> To this: <input type="submit" name="submit" value="Submit"> Quote Link to comment Share on other sites More sharing options...
tqla Posted June 28, 2007 Author Share Posted June 28, 2007 Ha Ha! I just figured that out too and was coming here to delete my last post. Thanks poco! Quote Link to comment Share on other sites More sharing options...
tqla Posted June 28, 2007 Author Share Posted June 28, 2007 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> Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted June 28, 2007 Share Posted June 28, 2007 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> Quote Link to comment Share on other sites More sharing options...
tqla Posted June 28, 2007 Author Share Posted June 28, 2007 <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? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted June 28, 2007 Share Posted June 28, 2007 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. Quote Link to comment Share on other sites More sharing options...
tqla Posted June 28, 2007 Author Share Posted June 28, 2007 Thank you so much ProjectFear! Thank you too pocobueno1388 and redarrow. Amazing, I learned a lot today! Humbly, tqla Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.