TeddyKiller Posted May 4, 2010 Share Posted May 4, 2010 A form, with a form action. onsubmit event calls a javascript function. Any function <script>function blah{}</script> Within the function, will contain no javascript. Just PHP. I feel this isn't quite ajax as it doesn't play around with more than 1 line of javascript. My question is.. would it be secure to do all my php form validation in that javascript function, using database queries, and without it reloading the page? Problem is.. I have an ahref, and it opens up a div, powered by javascript. When clicking the submit button... it reloads the page.. and the div disappears. I'd like it stayed open. If the form validation isn't the best method, maybe you can redirect me to something that is? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/200723-form-validation-with-next-to-no-javascript-but-without-refreshing-the-page/ Share on other sites More sharing options...
Pikachu2000 Posted May 4, 2010 Share Posted May 4, 2010 A client side technology should not be relied upon for form validation. Validation should be performed server-side. Quote Link to comment https://forums.phpfreaks.com/topic/200723-form-validation-with-next-to-no-javascript-but-without-refreshing-the-page/#findComment-1053261 Share on other sites More sharing options...
TeddyKiller Posted May 4, 2010 Author Share Posted May 4, 2010 It wouldn't be client side though. It'll just be a javascript function.. with PHP inside it.. that'd make it server side? Quote Link to comment https://forums.phpfreaks.com/topic/200723-form-validation-with-next-to-no-javascript-but-without-refreshing-the-page/#findComment-1053272 Share on other sites More sharing options...
Alex Posted May 4, 2010 Share Posted May 4, 2010 From what I understand what you're attempting to do is impossible. Can you elaborate? Quote Link to comment https://forums.phpfreaks.com/topic/200723-form-validation-with-next-to-no-javascript-but-without-refreshing-the-page/#findComment-1053276 Share on other sites More sharing options...
TeddyKiller Posted May 4, 2010 Author Share Posted May 4, 2010 Oh wait.. it would be client side, because if javascript was disabled on the browser.. the php wouldn't execute. Damn. Lets take facebook. The comment featuer on there.. when you comment on a post.. it validates it without reloading the page. I have something like this, but what it does.. is it calls a javascript function.. which calls a php page to do the validation. Although this wouldn't work if you have javascript disabled. (I found it off a tutorial site) You should know what I mean if you use facebook though. You fill in the textarea, click the submit button. Then the comment gets posted.. and there was no refreshing the page. Quote Link to comment https://forums.phpfreaks.com/topic/200723-form-validation-with-next-to-no-javascript-but-without-refreshing-the-page/#findComment-1053280 Share on other sites More sharing options...
Alex Posted May 4, 2010 Share Posted May 4, 2010 You can't execute PHP like that at all. PHP (PHP: Hypertext Preprocessor) is, as its name implies, only a preprocessor. Meaning that PHP is executed on the server side, only, and the output of that is sent to the browser. You simply can't embed PHP in JavaScript function. JavaScript is so widely supported these days it's really not important to consider about the extremely small percentage of people browsing the web that might not have JavaScript support. If I recall correctly, the statistics were estimated at under 5% several years ago. What you're looking for is AJAX (which isn't what you explained at all). The variables that you wish to submit and validate would be sent via the HTTP request made through AJAX. Your PHP script can then validate these variables and send the response back to JavaScript where the appropriate action can be taken. Quote Link to comment https://forums.phpfreaks.com/topic/200723-form-validation-with-next-to-no-javascript-but-without-refreshing-the-page/#findComment-1053290 Share on other sites More sharing options...
TeddyKiller Posted May 4, 2010 Author Share Posted May 4, 2010 Hmm.. was hoping to minimize the use of ajax. Ok so Ajax it is.. I know its the wrong section but do you a tutorial, or can you provide a basic thing of what I'm looking for. I'd like it to support ultiple errors. Hope its not too much to ask for, I'd post in the ajax section but it'd be kinda.. reposting. Quote Link to comment https://forums.phpfreaks.com/topic/200723-form-validation-with-next-to-no-javascript-but-without-refreshing-the-page/#findComment-1053295 Share on other sites More sharing options...
Alex Posted May 4, 2010 Share Posted May 4, 2010 You can find a good tutorial that covers the fundementals of AJAX here. If you're using a JavaScript library like jQuery, YUI, etc.. many of them include easy ways to implement AJAX. If you want to use JQuery you can look here for some good examples on how to use AJAX in JQuery. Quote Link to comment https://forums.phpfreaks.com/topic/200723-form-validation-with-next-to-no-javascript-but-without-refreshing-the-page/#findComment-1053302 Share on other sites More sharing options...
TeddyKiller Posted May 4, 2010 Author Share Posted May 4, 2010 Ah yeah. Methods like the JQuery one is one I have used before. Just can't fully understand it... Does this mean.. $.ajax({ url: 'mypage.php', success: function(data) { $('.result').html(data); alert('Load was performed.'); } }); If mypage.php had an echo. Would it get displayed on the page where the function is or what?[/code] Quote Link to comment https://forums.phpfreaks.com/topic/200723-form-validation-with-next-to-no-javascript-but-without-refreshing-the-page/#findComment-1053305 Share on other sites More sharing options...
Alex Posted May 4, 2010 Share Posted May 4, 2010 That would put the contents of the output in all elements of class "result". Quote Link to comment https://forums.phpfreaks.com/topic/200723-form-validation-with-next-to-no-javascript-but-without-refreshing-the-page/#findComment-1053310 Share on other sites More sharing options...
TeddyKiller Posted May 4, 2010 Author Share Posted May 4, 2010 Right. Ok thanks I think have sense of it now. url: "mypage.php", Does this enable things after the tag. So.. url: "mypage.php?act=something", It'll help in minimizing pages.. Quote Link to comment https://forums.phpfreaks.com/topic/200723-form-validation-with-next-to-no-javascript-but-without-refreshing-the-page/#findComment-1053315 Share on other sites More sharing options...
Alex Posted May 4, 2010 Share Posted May 4, 2010 That should be another parameter, like so: $.ajax({ url: 'mypage.php', data: {act: 'something'}, Quote Link to comment https://forums.phpfreaks.com/topic/200723-form-validation-with-next-to-no-javascript-but-without-refreshing-the-page/#findComment-1053329 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.