Jump to content

Form validation, with .. next to no javascript, but without refreshing the page


TeddyKiller

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.