dadamssg Posted January 22, 2009 Share Posted January 22, 2009 im creating a form and i want to use javascript to validate the input by checking it against regex's and displaying error messages but i want to processes and use the $_POST function to transfer the info...how do i go about enbedding a javascript function in my php document. i tried to just enter the script and save it as a php file but nothing happenned when i clicked the submit button... heres the javascript <script type = "text/javascript"> function validate(){ //get inputs title = document.getElementById("txtTitle").value; description = document.getElementById("txtDescription").value; location = document.getElementById("txtLocation").value; //console.log("Title: " + title + ", Description: " + description + ", Location: " + Location); // title must be at least ten characters if (title.match({10,80}) == null){ alert("Please enter a more descriptive Title."); } else { //name is OK, now check email //description must exist if (description == ""){ alert("Please enter a Description."); } else { //description is OK, now check local if (location == ""){ alert("Please enter a Location."); } else { } // end location if } // end description if } // end title if } // end function </script> and heres the button button type = "button" onclick = "validate()"> submit </button> Quote Link to comment https://forums.phpfreaks.com/topic/141872-javascript-in-php/ Share on other sites More sharing options...
.josh Posted January 22, 2009 Share Posted January 22, 2009 php is parsed on the server. javascript is parsed on the client. php looks at javascript as regular text to be output to the client. It doesn't care about it. You can use javascript to validate form fields and then have the info submitted by traditional methods (just clicking the form's send button, with the form's method set to 'post'), or you can use some ajax to send it to the server. Not that I would recommend clientside validation anyways. It is a trivial thing, even by script kiddie standards, to get around clientside validation. Quote Link to comment https://forums.phpfreaks.com/topic/141872-javascript-in-php/#findComment-742815 Share on other sites More sharing options...
dadamssg Posted January 22, 2009 Author Share Posted January 22, 2009 well im using this and it should work, but im getting an error on the "{" right under the first if statement....??? what am i doing wrong? if ( !ereg(".{10,80}",$_title) { print ("<script language = 'javascript'>alert('Please enter a more descriptive Title.');</script>"); } if ( $_description = "") { print("<script language = 'javascript'>alert('Please enter a Description.');</script>"); } if ( $_location = "") { print("<script language = 'javascript'>alert('Please enter a Location.');</script>"); } Quote Link to comment https://forums.phpfreaks.com/topic/141872-javascript-in-php/#findComment-742817 Share on other sites More sharing options...
.josh Posted January 22, 2009 Share Posted January 22, 2009 You're missing a closing quote. But you would have known that if you read the error. Quote Link to comment https://forums.phpfreaks.com/topic/141872-javascript-in-php/#findComment-742823 Share on other sites More sharing options...
dadamssg Posted January 22, 2009 Author Share Posted January 22, 2009 i DID read the error....i can't see where i need one though Quote Link to comment https://forums.phpfreaks.com/topic/141872-javascript-in-php/#findComment-742826 Share on other sites More sharing options...
.josh Posted January 22, 2009 Share Posted January 22, 2009 so you don't see on the very first line of the code you provided how you have 2 opening parenthesis but only 1 closing? Quote Link to comment https://forums.phpfreaks.com/topic/141872-javascript-in-php/#findComment-742829 Share on other sites More sharing options...
dadamssg Posted January 22, 2009 Author Share Posted January 22, 2009 ok well how how do i get a button to run a function..display the errors before actually doing the form action. heres what i got now <?php function errors() { if ( !ereg(".{10,80}",$_title) ) { print ("<script language = 'javascript'>alert('Please enter a more descriptive Title.');</script>"); } if ( $_description = "") { print("<script language = 'javascript'>alert('Please enter a Description.');</script>"); } if ( $_location = "") { print("<script language = 'javascript'>alert('Please enter a Location.');</script>"); } } ?> i don't think i have the button right <input type="submit" name="do" value="Submit" onclick = "errors()" > Quote Link to comment https://forums.phpfreaks.com/topic/141872-javascript-in-php/#findComment-742835 Share on other sites More sharing options...
.josh Posted January 22, 2009 Share Posted January 22, 2009 Like I said, you can't just intermingle php and javascript like that. PHP parses and sends results to the client. If you want server/client interaction without using a submit button, you would have to separate them and use ajax to make a request to a script and call functions based on what you send via ajax methods. Quote Link to comment https://forums.phpfreaks.com/topic/141872-javascript-in-php/#findComment-742837 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.