Jump to content
Larry Ullman's Book Forums

Recommended Posts

I started using php 4 years ago. Back then I read that to send data from a form to the php file that processes it, you need javascript to validate the data, and if you don't want to leave the current page, you need ajax to send the validated data to the second php file (I read this on a site that is very good for absolute beginners, so I won't name it) . I've been using this complicated technique ever since. I thought you had to use javascript just because otherwise the page would change and (thought I) the user couldn't modify the new page - except by going back.. Now, in chapter three I find out how simple this can all be done.

 

Am I missing something here? Is Ajax ever needed?

Link to comment
Share on other sites

Ajax is never "needed", per se, but it generally creates a more fluid experience for the end-user, while naturally making the development side of things more difficult for the developer.

To answer your question clearly though: Ajax is not required for form processing.

 

All form processing can be easily handled with only PHP.

In general though, it's nice to integrate both JS and Ajax into a site to make everything smoother and quicker for the user.

Link to comment
Share on other sites

Never used the validate.js plug-in before, but that sounds like the quick and dirty solution (not to suggest that it's no good).

Obviously, whenever possible and whenever time permits, writing it yourself gives you the ultimate in flexibility and customization for the best possible experience, but that is obviously the harder route.

 

So to answer your question, if I didn't have time, I'd use the validation library, and if I had the time and inclination, I'd do it myself.

  • Upvote 1
Link to comment
Share on other sites

Can I ask a slighty related question (I'm not sure if I should really be making a new topic) I am looking to create a php page where the HTML and Jquery is printed or echoed in the page

 

For example

<?php
print "All Jquery code";
?>

 

I have heard that you can break out of the php code like the following

 

<?php
My PHP Code
?>
Jquery Code
<?php
More PHP code
?>

 

But I would rather have the Jquery printed out in PHP and not breaking out of the PHP tags <?php ?>

 

Any Ideas ?

Link to comment
Share on other sites

I'm editing my original post, because what I said was just flat-out wrong. Sorry.

Basically, you cannot send a JS/jQuery value to PHP by breaking up PHP tags. It doesn't work.

If you read that post, please disregard it and note the following instead.

 

To send a jQuery (or Javascript) value to a PHP script, you have two choices:

1) Call a PHP script and send the values in the URL to that script, which can then be retrieved from the $_GET superglobal, or

2) Use Ajax to essentially do the same thing as in 1).

 

The benefit of Ajax is that it allows you to call the PHP script without reloading the page. The disadvantage is that it is more complex.

Luckily, if you are using jQuery, you can call the ajax method, which does all the work for you.

 

If you go the first route, you have a couple of options for ways to reload the page. One thing you can do is use the window.location object to dynamically change the URL to the PHP script with the necessary parameters in the URL. Of course, you will have to have a way of redirecting back to the original page from the PHP script (if it's not the same page to begin with).

 

Anyway, I perhaps didn't provide the best explanation, but that's the answer to your question. If you're still confused, I might recommend starting a new topic on either this board or the Modern Javascript board.

Thanks.

Link to comment
Share on other sites

 Share

×
×
  • Create New...