开发者

Call jquery from php

I have a form with Name : input and a submit. When pressed, it posts to the same php file. My first check is basically if(!$name) { call jquery to insert error class }. I have the jquery set up in a function but I'm not sure how to call the function from the i开发者_Python百科f statement.


You need to do your check in javascript / jquery and avoid posting to the php file until the javascript validation is completed / satisfactory.

Then in php you need to validate again in case the visitor has javascript disabled.


Don't use jquery in this case. Just have PHP output the appropriate class in the HTML, since PHP can not directly (or even indirectly) call javascript functions:

<?php
   $name_error = empty($name); // $name_error is true/false;
?>

[...snip...]

<div class="this and that <?php if ($name_error) { echo 'error classname here'; } ?>">
    <?php if ($name_error) { echo 'error message here'; } ?>
</div>

Trying to get PHP to call javascript to do what PHP can already do perfectly well on the server is a waste of effort. It's like driving to a payphone instead of using the perfectly good phone that's already sitting on your desk.


I think my answer for this question should prove helpful.

In a nutshell - your PHP script will need to send data back to the client that will let you identify which field is in error and why. jQuery will then be responsible for altering the field as you see fit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜