rocky_88 Posted March 29, 2011 Share Posted March 29, 2011 I am an absolute beginner in php & am trying to validate a form using javascript. Problem is I am checking if the textbox is left empty before the user clicks on the submit button(using javascript). On the other hand i'm using isset function of php for the same submit button so that it does not run the code unless and until the button is clicked. Due to this, the default value of text box is being entered into database. How do i prevent this? Is there an alternate function to isset for such an event? Here is a sample code <?php if(isset($_POST[sub])) $sql="insert into testing values('$_POST[txt1]')"; $exec = mysql_query($sql); ?> <script type="text/javascript"> function check() { var a = document.getElementById('add'); if (a.value = "") { return false; } } </script> <form action="add.php" method="post"> <input type="text" name="txt1" id="add"><br> <input type="submit" name="sub" value="Click to Add" onclick = "check();"> </form> I'm using ghost text technique on my page instead of labels so the text boxes have different values at different instances and hence I cant use !empty function either. How do i unset the submit button so that it does not run the script further? Quote Link to comment Share on other sites More sharing options...
jamesjmann Posted March 29, 2011 Share Posted March 29, 2011 I am an absolute beginner in php & am trying to validate a form using javascript. Problem is I am checking if the textbox is left empty before the user clicks on the submit button(using javascript). On the other hand i'm using isset function of php for the same submit button so that it does not run the code unless and until the button is clicked. Due to this, the default value of text box is being entered into database. How do i prevent this? Is there an alternate function to isset for such an event? For the javascript portion: You're going to have to run an if statement that checks to see if the default value in the field has been left untouched, or if that field is blank. If so, execute error message. As for the php...if you have text already inside the box when the page loads, you're going to have to write an if statement in php, quite similar to the one I mentioned above. The code would look this: <?php //Check to see if the textbox element was passed to the $_POST array if (isset ($textbox)) { if ($textbox == "Default text in form") { echo "You did not enter any text."; } else { mysql_query ($query); } } else { echo "For some impossible reason, the textbox did not get passed to the \$_POST array."; } ?> Doesn't get any simpler than that! Now if you want to make sure the javascript catches the error before submit is clicked, you're going to want to make sure that if statement within your function gets called ONLY onSubmit (the attribute that goes in the form) and onClick (when the user clicks "submit"). To ensure the form doesn't get submitted until the errors have been corrected, you have to give the "onSubmit" and "onClick" attributes a value of "return checkForm_function();" In your function you would create a var called "var status" where status is equal to true or false (don't declare it either initially, but make sure it is declared nonetheless...). Then, when you write your if statement, you would say: If ($("#var").val() == "default text in form") { status = false; $("#error_msg_box").fadeIn(500); } else { status = true; $("#error_msg_box").fadeOut(500); } So when you hit submit, the document will run the function with the if statement above, and if that user did not type over the default text, a div with an error message inside would slowly fade in, and the form would become "false", disabling the "action" on it. As soon as that error gets fixed, however, the form will be submittable. You can also add additional features to your form, such as "onKeyUp" and "onKeyDown" and "onChange" and "onBlur", and for each attribute you would assign a value of "checkForm_function();" This would make it to where whenever a user types a character or changes to another field or makes current field go out of focus, that function will execute and display error messages on the fly (or in "realtime", i suppose you can say). Hope this helped! NOTE: The above javascript code is in fact JQuery. If you are unfamiliar with the library, I highly recommend you invest time in learning it, as it will make life a lot easier for you when scripting. Quote Link to comment Share on other sites More sharing options...
rocky_88 Posted March 29, 2011 Author Share Posted March 29, 2011 @jamesjmann Thanks for your quick reply, The problem here is everything works just fine, except the fact that when ever i click on the submit button, the isset function returns true and executes the codes. I will be using about 9 to 10 text boxes and will be using javascript to check whether or not they r left empty/untouched. That part is working fine and it does what it has to. But regardless of the validation, due to isset function, the empty values are getting inserted into the database as soon as i click on submit. Basically i want to trigger isset function of php using javascript. Like it should not set it to true unless and untill all the conditions are satisfied by javascript. Quote Link to comment Share on other sites More sharing options...
beegro Posted March 29, 2011 Share Posted March 29, 2011 Hi rocky, The problem you're having is that the isset() function is always evaluating to TRUE because it sees that there is a value for $_POST['sub']. By nature, when you post a form PHP receives all of the form elements with 'name' attributes in the HTML. Therefor, the check you are doing isset($_POST['sub']) is invalid because it will always evaluate to TRUE when pressing the submit button. I think the problem you're having is not in the PHP but in the javascript. To stop your form from being submitted when the javascript evaluates to false, I'd suggest moving the javascript call to the onsubmit event of the form itself. <form action="add.php" method="post" onsubmit="return check();"> This should allow the return value from the javascript function to override the default action from the "onsubmit" event. Don't forget that "return" command. Quote Link to comment Share on other sites More sharing options...
jamesjmann Posted March 29, 2011 Share Posted March 29, 2011 @jamesjmann Thanks for your quick reply, The problem here is everything works just fine, except the fact that when ever i click on the submit button, the isset function returns true and executes the codes. I will be using about 9 to 10 text boxes and will be using javascript to check whether or not they r left empty/untouched. That part is working fine and it does what it has to. But regardless of the validation, due to isset function, the empty values are getting inserted into the database as soon as i click on submit. Basically i want to trigger isset function of php using javascript. Like it should not set it to true unless and untill all the conditions are satisfied by javascript. Okay, I'm pretty sure I just solved your problem, but allow me to reexplain... First off, the isset function checks to see if a field has a set value. Technically, you don't need it, but it's always good to have it. Also - REMEMBER THAT ISSET IS NOT GOOD FOR COMPARING VARIABLES OR SATISFYING CONDITIONS. YOU ONLY USE ISSET TO CHECK IF THE FIELD ELEMENT EXISTS, NOT IF A FIELD ELEMENT CONTAINS AN EMPTY STRING. So, if you DONT want the empty fields to be submitted until there is something in them, again, you would write an if statement: //Check to see if the field exists, and obviously it will once submit has been pressed if (isset ($field)) { //Do actual validation of form if ($field == "default value") { //SHOW MESSAGE, DO NOTHING ELSE! } else { //INSERT INTO DATABASE } if ($field == "") { //SHOW MESSAGE, DO NOTHING ELSE! } else { //INSERT INTO DATABASE } } The above code ensures that the text box does not contain the default placeholder text and is not empty (VIA PHP) - and I'm pretty sure I already explained all this earlier, but anyway... Quote Link to comment Share on other sites More sharing options...
rocky_88 Posted March 30, 2011 Author Share Posted March 30, 2011 @beegro PERFECT, calling the onsubmit event on the form instead of the button worked perfectly, Thanks a lot @jamesjmann Thanks for re-explaining it, Problem was that i was gonna use more than one text box in this coding, so i did not want to use if soo many times to check each and every text box, so instead i used javascript to check the contents of the text box, validate it and submit ONLY if it satisfies the condition, changing the onlcick event from button to onsubmit event of the form worked like a charm. Sorry if I wasnt clear with my words. Quote Link to comment Share on other sites More sharing options...
trq Posted March 30, 2011 Share Posted March 30, 2011 Even though you are using JavaScript to validate you should always do another round of validation server side using PHP. JavaScript can be disabled and it should not be relied upon for functionality. Quote Link to comment Share on other sites More sharing options...
rocky_88 Posted March 30, 2011 Author Share Posted March 30, 2011 @beegro Can i not use the same type of validation on a button instead of stopping the whole form from being submitted? Is there any function other than isset() to check if the button was clicked that would help to achieve that? PS : The solution worked perfectly, i just want to know if there is a possibility to make it work in the same way by using button. Quote Link to comment Share on other sites More sharing options...
rocky_88 Posted March 30, 2011 Author Share Posted March 30, 2011 @thorpe Thanks for the suggestion, I know that javascript can be disabled, but is validating soo many text boxes at server side using if statements every time some one submits a form recommended? Wouldnt it put a lot of load on server? Quote Link to comment Share on other sites More sharing options...
trq Posted March 30, 2011 Share Posted March 30, 2011 is validating soo many text boxes at server side using if statements every time some one submits a form recommended? Of course it's recommended. If you want the data, you must ensure you have it and it looks like what your expecting. Wouldnt it put a lot of load on server? It's not even worth considering. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 30, 2011 Share Posted March 30, 2011 If you don't validate on the server side, you will be sorry. PHP is running on a server that is usually much more powerful than your browser which is running the Javascript code. If you code your PHP smartly, you may not have to use many if statements, you might be able to use switch statement or something else. If you show us your real form, since the one you posted has only one field, we might be able to suggest some efficient code. Ken Quote Link to comment Share on other sites More sharing options...
rocky_88 Posted March 30, 2011 Author Share Posted March 30, 2011 @thorpe Thank you for the reply, Actually i was using basic labels with text box and with that i was using !empty(). My tutor however, recommended otherwise saying that validations such as the value entered and if a field is empty or not should be done using client side scripting language such as javascript as the server will be heavily loaded if 1000 people are at a time submitting the form. @kenrbnsn Thanks for the suggestion, The real form is as same as this, only difference being instead of one text box I am using 6. Using it to enter User name, Login name, Password, Age, Email ID & Contact Number. I may add few more fields later on and hence wanted to use a method which puts the least load on server atleast during validating the values entered. Quote Link to comment Share on other sites More sharing options...
trq Posted March 30, 2011 Share Posted March 30, 2011 My tutor however, recommended otherwise saying that validations such as the value entered and if a field is empty or not should be done using client side scripting language such as javascript as the server will be heavily loaded if 1000 people are at a time submitting the form. Not wanting to put a damper on things but it sounds to me like your tutor has little experience building web applications. Quote Link to comment Share on other sites More sharing options...
rocky_88 Posted March 30, 2011 Author Share Posted March 30, 2011 @thorpe So the recommended way of validating data to be entered is by using php? If yes can u help me on how to do it more dynamically if there are more fields on the form? Quote Link to comment Share on other sites More sharing options...
jamesjmann Posted March 30, 2011 Share Posted March 30, 2011 @thorpe So the recommended way of validating data to be entered is by using php? If yes can u help me on how to do it more dynamically if there are more fields on the form? I would use arrays to store the messages you want displayed when an error occurs. This provides for easier future editing. Also, i came up with this concept where theres error objects and an "error report". Basically what you could do is write a list of if/else statements checking the various aspects of the form; and if something happens that you dont want, you would create a variable and set it to "true" (which is essentially your 'error object'). You'd also want to create a variable called $error_report (which is used later on in your script). And You would want an error object for every field and every aspect of each field. Each would carry a unique "error object" name but all of them would contain the same "error report". Then, you would create an action script that decides what to do once the form has been submitted and checked. This is where the "error report" comes in. If ($error report) { display_form(); } else { //do mysql query or whatever } Quote Link to comment Share on other sites More sharing options...
rocky_88 Posted March 30, 2011 Author Share Posted March 30, 2011 @jamesjmann Thanks for the suggestion, I'll try to implement it 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.