karimali831 Posted April 14, 2010 Share Posted April 14, 2010 Hi I want to prevent a user from submitting a form if a value in a text field is the same in another text field. anyways of doing this? thanks for help. Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted April 14, 2010 Share Posted April 14, 2010 Yep, but not through HTML. What you're describing is basic form validation and you can achieve this using server-side (PHP) or client-side (javascript). Google "php form validation" or "javascript form validation". For PHP, you simply need to define the input of your text fields as variables and then compare them using a Boolean condition. I can do this for you if you paste the relevant code or explain what it is you're trying to achieve. Quote Link to comment Share on other sites More sharing options...
karimali831 Posted April 15, 2010 Author Share Posted April 15, 2010 Hi, Here is the HTML: <form action="?site=cupactions&action=score&clan1=$clanID&matchID=$matchID&cupID=$cupID" method="post" name="post"> <table width="100%" cellspacing="0" border="0" cellpadding="2" bgcolor="$border"> <tr> <td bgcolor="$pagebg"></td> </tr> <tr> <td bgcolor="$bg1"> <table width="100%" border="0" align="center" cellpadding="2" cellspacing="0"> <tr> <td>%your% Score:</td> <td><input name="score1" type="text" class="form_off" id="score1" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'" size="3"></td> <td>%opponent% Score: <input name="score2" type="text" class="form_off" id="score2" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'" size="3"></td> </tr> <tr> <td colspan="3" align="right" ><input name="submit" type="submit" value="%submit%"></td> </tr> </table> </td> </tr> </table> </form> For validations I want numeric values only and if score1 is equal/same as score 2, there will be an error. Thanks for your help Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted April 15, 2010 Share Posted April 15, 2010 Howdy, As much as I’ve wanted to, I haven’t significantly edited your html mark-up or added CSS. You really shouldn’t be using a table, let alone a table within a table to define your layout. Tables are for presenting tabular data such as a database. Instead, you should be using divs styled by CSS. Learn more here: http://www.w3.org/Style/CSS/ In any case, I’m by no means a PHP expert but the following validation works - just copy and paste: <?php // If the submit button is pressed... if (isset($_POST['submit'])) { // Validation // If score one is empty or non-numeric define a message as a variable... if (strlen($_POST['scoreone']) > 0 && is_numeric($_POST['scoreone'])) {$scoreone=TRUE;} else {$scoreone=FALSE; $message_scoreone=" *Error! Please input a numeric value for score one!";} // If score two is empty or non-numeric define a message as a variable... if (strlen($_POST['scoretwo']) > 0 && is_numeric($_POST['scoretwo'])) {$scoretwo=TRUE;} else {$scoretwo=FALSE; $message_scoretwo=" *Error! Please input a numeric value for score two!";} // If score one = score two and are not blank define a message as a variable: if (($_POST['scoreone'] == $_POST['scoretwo']) && (strlen($_POST['scoreone']) > 0) && (strlen($_POST['scoretwo']) > 0)) {$scorecheck=FALSE; $message_scorecheck=" *Error! Please ensure that score one and two are not the same!";} else {$scorecheck=TRUE; } // If score one and two are validated and do not match then pass the variables via url... // THE HEADER FUNCTION MUST BE POSITIONED BEFORE THE OPENING HTML TAG!!! if ($scoreone && $scoretwo && $scorecheck) { //WE'RE USING THE HEADER FUNCTION INSTEAD OF THE FORM TO SEND THE VARIABLES VIA URL... header('Location:?site=cupactions&action=score&clan1=$clanID&matchID=$matchID&cupID=$cupID'); } } ?> <html> <body style="color: white; font-family: Arial, sans-serif;"> <!--<form action="?site=cupactions&action=score&clan1=$clanID&matchID=$matchID&cupID=$cupID" method="post" name="post">--> <form action="<?php echo $_SERVER['PHP_SELF']; ?>#marker" method="post" enctype="multipart/form-data"> <!--START OUTER TABLE--> <table width="100%" cellspacing="0" border="0" cellpadding="2" bgcolor="$border"> <tr> <td bgcolor="$pagebg"> </td> </tr> <tr> <td bgcolor="$bg1"> <!--START INNER TABLE--> <table width="100%" border="0" align="center" cellpadding="2" cellspacing="0"> <tr> <td> %your% Score: </td> <td> <input name="scoreone" type="text" class="form_off" id="score1" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'" size="3" value="<?php if (isset($_POST['scoreone'])) echo $_POST['scoreone']; ?>"> </td> <td> %opponent% Score: <input name="scoretwo" type="text" class="form_off" id="score2" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'" size="3" value="<?php if (isset($_POST['scoretwo'])) echo $_POST['scoretwo']; ?>"> </td> </tr> <tr> <td colspan="2" style="color:red; padding-left: 10px; font-family: Arial, sans-serif;"> <?php if ($message_scoreone) echo "<p>".$message_scoreone."</p>"; ?> <?php if ($message_scoretwo) echo "<p>".$message_scoretwo."</p>"; ?> <?php if ($message_scorecheck) echo "<p>".$message_scorecheck."</p>"; ?> </td> <td colspan="1" align="right"> <input name="submit" type="submit" value="%submit%"> </td> </tr> </table> <!--END INNER TABLE--> </td> </tr> </table> <!--END OUTER TABLE--> </form> <body> </html> The form will now do one of four things: [*]Produce an error message if score one is not empty numeric. [*]Produce an error message if score two is empty or not numeric. [*]Produce an error if score one and two are the same. [*]If there are no errors the form will behave as usual. Pay attention to the comments I included within the code. Certain functions, such as the header() function should not be moved. Mark this topic as solved once you're good to go and for future reference, this should have been posted in the PHP coding help section of the forum. Quote Link to comment Share on other sites More sharing options...
karimali831 Posted April 15, 2010 Author Share Posted April 15, 2010 Thanks very much Got the validations to work but not the form submit. The template is on a HTML file so I can't use php? Quote Link to comment Share on other sites More sharing options...
karimali831 Posted April 15, 2010 Author Share Posted April 15, 2010 I've got it, thanks very much once again. Quote Link to comment Share on other sites More sharing options...
karimali831 Posted April 15, 2010 Author Share Posted April 15, 2010 I think there is a bug. Everytime I use if (strlen($_POST['score1']) > 0 && is_numeric($_POST['score1'])) {$score1=TRUE;} else {$score1=FALSE; $message_score1=" *Error! Please input a numeric value for score 1!";} if (strlen($_POST['score2']) > 0 && is_numeric($_POST['score2'])) {$score2=TRUE;} else {$score2=FALSE; $message_score2=" *Error! Please input a numeric value for score two!";} if (($_POST['score1'] == $_POST['score2']) && (strlen($_POST['score1']) > 0) && (strlen($_POST['score2']) > 0)) {$scorecheck=FALSE; $message_scorecheck=" *Error! Please ensure that score one and two are not the same!";} else {$scorecheck=TRUE; } if ($message_score1) die("$message_score1"); if ($message_score2) die("$message_score2"); if ($message_scorecheck) die("$message_scorecheck"); and I enter for example, 1 for score1 and 0 for score2, in database it shows score1 is 0 and score2 is 0? when i don't use the above it's fine. Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted April 15, 2010 Share Posted April 15, 2010 That's sounds a bit odd but it's very likely a minor issue. Can you reveal more of your code (the more the better) so that I can try to reproduce what you're trying to achieve on my end? Naturally edit out your database password and any other sensitive information with an 'x'. I'm guessing it's how the SQL query is picking up on the values of the variables. Quote Link to comment Share on other sites More sharing options...
karimali831 Posted April 15, 2010 Author Share Posted April 15, 2010 I fixed it now. The validation was below the select query and suppose to be above it if I'm making sense. Quote Link to comment Share on other sites More sharing options...
webmaster1 Posted April 15, 2010 Share Posted April 15, 2010 Good stuff. (Don't forget to mark solved if good to go) 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.