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 if statement.

- 222,467
- 53
- 283
- 367

- 3,301
- 5
- 22
- 25
-
can you post the code you have so far, then we can start from it. – tkt986 Jan 10 '11 at 22:05
-
7Jquery runs in the browser. PHP runs on the server. You can't directly call one from the other. You can use php to `echo "` that will be executed when the browser gets it. – Byron Whitlock Jan 10 '11 at 22:06
-
You can't have "PHP call jQuery", the both are running completely different environments. Could you explain a little further what it is that you're trying to achieve? – polarblau Jan 10 '11 at 22:07
-
all i want to do is when the php script has nothing in the box is add the class error but i need to know how to call my function – Rod Nelson Jan 10 '11 at 22:13
-
i tryed using the echo " i put my function name in the place of the some jquery code and all the the script has in it atm is alert('it worked'); but firefox dont seem to work with it i havent tried it in IE – Rod Nelson Jan 10 '11 at 22:18
3 Answers
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.

- 356,200
- 43
- 426
- 500
-
i dont know why you hate jquery. i realy dont want to use a tone of code every tie i want to parse a text box on my regrestion page! – Rod Nelson Jan 10 '11 at 22:28
-
i played with this it works kinda i just need to make a function that dose this work for me so i dont have to add this code 20 times to parse my form thx – Rod Nelson Jan 10 '11 at 22:36
-
I have nothing against jquery. It's a wonderful tool. But you cannot invoke Javascript from PHP. PHP executes on the server, javascript executes on the client. – Marc B Jan 11 '11 at 01:46
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.

- 91,079
- 21
- 114
- 132
-
i was playing with trying to call the function manually and my problem is the java Script is below the php script but i may be able to write a php function to do the same thing from what the other person posted that may just be the easier way! – Rod Nelson Jan 10 '11 at 22:30
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.

- 1
- 1

- 105,256
- 31
- 182
- 206
-
i planed on doing that i just figured it would be easier to start with a static ID then work from there. Im thinking of Rewriting my website in RUBY any ways its so much more user friendly once you learn it – Rod Nelson Jan 10 '11 at 23:03