Calling jQuery within PHP
10-12-2009, 12:44 AM
|
Calling jQuery within PHP
|
Posts: 19
|
Hey guys,
Hopefully this is possible. I was wondering if it was possible to call jQuery withing PHP. For example:
Code:
<?php
if(this == that) {
//do something in php
} else {
//do something with jquery (i.e. fade something in/out
}
?>
Does that make sense? Please let me know. Thanks.
|
|
|
|
10-12-2009, 01:13 AM
|
Re: Calling jQuery within PHP
|
Posts: 64
Name: Taminder B.
Location: San Jose, CA
|
you can echo the HTML code like you normally would.
__________________
Please login or register to view this content. Registration is FREE
(HTML/PHP/MySQL/JavaScript/AJAX/SEO)
|
|
|
|
10-12-2009, 01:25 AM
|
Re: Calling jQuery within PHP
|
Posts: 2,784
Name: Matt
Location: Irvine, CA
|
You can't directly call jquery functions from PHP. PHP is server side code; it is completely unaware of any client side code such as HTML, CSS, or Javascript. You can, however, output javascript just as you would HTML code.
|
|
|
|
10-12-2009, 03:23 AM
|
jQuery within PHP script
|
Posts: 19
|
Hey guys,
I am making a message board for fun. In my message board I have a script that checks if the user has filled in all the fields and if so, inserts the data into a database. Now what I'm trying to do is if the user has not entered text in every field, I want a pop up to appear preferably using jQuery.
Here is my current PHP code:
Code:
if(isset($_POST['submit'])) {
$title = trim($_POST['title']);
$author = trim($_POST['author']);
$message = trim($_POST['message']);
if($title != "" && $author != "" && $message != "") {
$sql = "INSERT INTO threads (title, message, author, posted)
VALUES('$title', '$message', '$author', NOW())";
mysql_query($sql) or die(mysql_error());
$last_id = mysql_insert_id();
echo $last_id;
header("Location: viewthread.php?id=$last_id");
} else {
//POP UP SHOULD HAPPEN HERE
}
}
Is this possible? If so, how would I start to go about doing this? Thank you in advance!
|
|
|
|
10-12-2009, 04:17 AM
|
Re: jQuery within PHP script
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
You miss 1 important point:
PHP is server based, and run at the page generation time
Javascript (which jQuery is) is client side, and is run after the page has been generated and sent to the client.
So, yes, it's possible, but not exactly like that.
In your "else", you have to create a bit of javascript that will be added in the page, to signal that there was something missing.
PHP Code:
if(isset($_POST['submit'])) { $title = trim($_POST['title']); $author = trim($_POST['author']); $message = trim($_POST['message']); if($title != "" && $author != "" && $message != "") { $sql = "INSERT INTO threads (title, message, author, posted) VALUES('$title', '$message', '$author', NOW())"; mysql_query($sql) or die(mysql_error()); $last_id = mysql_insert_id(); echo $last_id; header("Location: viewthread.php?id=$last_id"); } else { //POP UP SHOULD HAPPEN HERE echo <<<html <script type="text/javascript"> var missingSomething=true; </script> html;
} }
And then, in your page, you have to have a bit of javascript that check if the "missingSomething" variable exists, and show the popup.
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
10-12-2009, 04:32 AM
|
Re: jQuery within PHP script
|
Posts: 19
|
Thank you. I understand that PHP is server side and JavaScript is client side. I'm not that fluent with JavaScript however. I understand the concept cause I understand programming, I just don't know where to get started. Could anyone possibly show me how to do this? Or at least a link that would?
Thank you.
|
|
|
|
10-12-2009, 04:48 AM
|
Re: jQuery within PHP script
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
I never used jquery, but from where I left you before, it's just
Code:
if(varMissing===true){
show_popup()
}
that should be called after the output of the javascript from PHP.
I believe that every js framework today are waiting to the DOM tree to be ready before doing anything, so it (waiting to see the generated missingSomething variable) should be handled natively by the framework.
After that, you only have to crawl through http://docs.jquery.com/UI/Dialog to create your popup.
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
10-12-2009, 06:52 AM
|
Re: jQuery within PHP script
|
Posts: 129
Name: Sidney shieldon
|
The coding:
if(isset($_POST['submit'])) {
$title = trim($_POST['title']);
$author = trim($_POST['author']);
$message = trim($_POST['message']);
if($title != "" && $author != "" && $message != "") {
$sql = "INSERT INTO threads (title, message, author, posted)
VALUES('$title', '$message', '$author', NOW())";
mysql_query($sql) or die(mysql_error());
$last_id = mysql_insert_id();
echo $last_id;
header("Location: viewthread.php?id=$last_id");
} else {
//POP UP SHOULD HAPPEN HERE
echo <<<html
<script type="text/javascript">
var missingSomething=true;
</script>
html;
}
}
For pop up:
if(varMissing===true) {show_popup()}
practice it and you will be telling others how to do that...
|
|
|
|
10-12-2009, 06:59 AM
|
Re: Calling jQuery within PHP
|
Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
|
Two threads on same topic merged.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
10-12-2009, 11:42 AM
|
Re: Calling jQuery within PHP
|
Posts: 19
|
Hey guys,
Here is what I have so far, it does not seem to be working.
Code:
if(isset($_POST['submit'])) {
$title = trim($_POST['title']);
$author = trim($_POST['author']);
$message = trim($_POST['message']);
if($title != "" && $author != "" && $message != "") {
$sql = "INSERT INTO threads (title, message, author, posted)
VALUES('$title', '$message', '$author', NOW())";
mysql_query($sql) or die(mysql_error());
$last_id = mysql_insert_id();
echo $last_id;
header("Location: viewthread.php?id=$last_id");
} else {
echo "<script type=\"text/javascript\">
var popUp == true;
</script>";
}
}
And then for my javascript I have:
Code:
<script type="text/javascript">
if(popUp==true) {
showDialog('Error', 'You must enter information in to all the fields!', 'error');
}
</script>
It isnt working though. Where do I need to place my javascript code on my page. I have tried it in the header and below the php. Any help would be awesome. Thanks.
|
|
|
|
10-12-2009, 11:46 AM
|
Re: Calling jQuery within PHP
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
is surely not what you want to do
= is an assignation. (x='a')
== is a test, with casting between type accepted (y==z where y=1 and z='1' returns true)
=== is a test, with a strict type check (y===z where y=1 and z='1' returns false)
In this case, you tells the javascript engine that you want to check that the value of popUp is equal to true, when you'd want to assign the value true.
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
10-12-2009, 12:17 PM
|
Re: Calling jQuery within PHP
|
Posts: 19
|
I have tried it with:
This has given me no luck, so I was messing with other ways. Either way it still does not work. I have a javascript file linked in the head of my document that contains the function showDialog(). I am just wondering where I have to put:
Code:
<script type="text/javascript">
if(popUp==true) {
showDialog('Error', 'You must enter information in to all the fields!', 'error');
}
</script>
I have tried putting it both in the <head> tags and below the php script. Also, is there more too it? Please let me know. Thanks
Last edited by pdpullmn612; 10-12-2009 at 12:18 PM..
|
|
|
|
10-12-2009, 02:33 PM
|
Re: Calling jQuery within PHP
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
As I have stated before, you have to make sure that the javascript is fired after you have outputed the "
var popUp = true;" part.
Javascript is interpreted as it's seen, and not when the page is completely loaded, except if you tell it to load on page load with something like this:
HTML Code:
function isLoaded(){
//Ok, page loading done, we launch what we need.
//Do your stuff here
if(popUp==true){
showDialog('Error', 'You must enter information in to all the fields!', 'error');
}
}
try{
window.addEventListener('load', isLoaded,True);
} catch(r){
//blody IE !
window.attachEvent('onload', isLoaded);
}
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
10-12-2009, 02:46 PM
|
Re: Calling jQuery within PHP
|
Posts: 744
Name: Mattias Nordahl
Location: Sweden
|
I believe the jQuery library uses this syntax
Code:
$(document).ready(function() {
// Your code here
});
This piece of code makes sure no code is run before the page is ready, and should surrond all your jQuery code. There is also a shorter version of it, since it is used so often. If I remeber correctly it's like this.
Code:
$(function() {
// Your code here
});
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
|
|
|
|
10-12-2009, 06:13 PM
|
Re: Calling jQuery within PHP
|
Posts: 19
|
Ok thanks guys. Still don't understand it 100% as I'm not that fluent with javascript, but I guess I'll try and see what I can do.
|
|
|
|
|
« Reply to Calling jQuery within PHP
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|