Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Closed Thread
Calling jQuery within PHP
Old 10-12-2009, 12:44 AM Calling jQuery within PHP
Average Talker

Posts: 19
Trades: 0
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.
pdpullmn612 is offline
View Public Profile
 
 
Register now for full access!
Old 10-12-2009, 01:13 AM Re: Calling jQuery within PHP
rednimaT's Avatar
Skilled Talker

Posts: 64
Name: Taminder B.
Location: San Jose, CA
Trades: 0
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)
rednimaT is offline
View Public Profile Visit rednimaT's homepage!
 
Old 10-12-2009, 01:25 AM Re: Calling jQuery within PHP
NullPointer's Avatar
Will Code for Food

Posts: 2,784
Name: Matt
Location: Irvine, CA
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
View Public Profile Visit NullPointer's homepage!
 
Old 10-12-2009, 03:23 AM jQuery within PHP script
Average Talker

Posts: 19
Trades: 0
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!
pdpullmn612 is offline
View Public Profile
 
Old 10-12-2009, 04:17 AM Re: jQuery within PHP script
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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.
tripy is offline
View Public Profile Visit tripy's homepage!
 
Old 10-12-2009, 04:32 AM Re: jQuery within PHP script
Average Talker

Posts: 19
Trades: 0
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.
pdpullmn612 is offline
View Public Profile
 
Old 10-12-2009, 04:48 AM Re: jQuery within PHP script
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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.
tripy is offline
View Public Profile Visit tripy's homepage!
 
Old 10-12-2009, 06:52 AM Re: jQuery within PHP script
infotech rules's Avatar
Super Talker

Posts: 129
Name: Sidney shieldon
Trades: 0
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...

infotech rules is offline
View Public Profile
 
Old 10-12-2009, 06:59 AM Re: Calling jQuery within PHP
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
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?
chrishirst is offline
View Public Profile Visit chrishirst's homepage!
 
Old 10-12-2009, 11:42 AM Re: Calling jQuery within PHP
Average Talker

Posts: 19
Trades: 0
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.
pdpullmn612 is offline
View Public Profile
 
Old 10-12-2009, 11:46 AM Re: Calling jQuery within PHP
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Code:
var popUp == true;
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.

Code:
var popUp = true;
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
View Public Profile Visit tripy's homepage!
 
Old 10-12-2009, 12:17 PM Re: Calling jQuery within PHP
Average Talker

Posts: 19
Trades: 0
I have tried it with:

Code:
var popUp = true;
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..
pdpullmn612 is offline
View Public Profile
 
Old 10-12-2009, 02:33 PM Re: Calling jQuery within PHP
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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.
tripy is offline
View Public Profile Visit tripy's homepage!
 
Old 10-12-2009, 02:46 PM Re: Calling jQuery within PHP
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
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
lizciz is online now
View Public Profile Visit lizciz's homepage!
 
Old 10-12-2009, 06:13 PM Re: Calling jQuery within PHP
Average Talker

Posts: 19
Trades: 0
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.
pdpullmn612 is offline
View Public Profile
 
Closed Thread     « Reply to Calling jQuery within PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.76001 seconds with 12 queries