Calling Jscrict

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Calling Jscrict

Post by spacebiscuit »

Hi,

I am using some javascript that is called when a link is clicked as follows:

<a class="modal" href="#">A link</a>

However I want to call the javascript if a php variable is not set, is this possible?

Thanks.
Last edited by spacebiscuit on Thu Mar 15, 2012 3:46 am, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Calling Jscrict

Post by requinix »

Uh... if it's not set? The quick answer is no. JavaScript and PHP are completely separate. You can't mix and match them. The line between them can be a bit blurry at times, but there's a line.

How about explaining what this is about? What variable? What JavaScript?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Calling Jscrict

Post by califdon »

spacebiscuit wrote:Hi,

I am using some javascript that is called when a link is clicked as follows:

<a class="modal" href="#">A link</a>

However I want to call the javascript of a php variable is not set, is this possible?

Thanks.
I don't understand your question. The link you showed there has nothing in it related to Javascript. And, as requinix said, "the javascript of a php variable" has no meaning. The only relationship between PHP and Javascript is that PHP can be used to generate some Javascript code that is sent to the browser. Also, using the Javascript object called XMLHTTPRequest, a request for data can be sent to a PHP script that runs on the server; this is called AJAX. What are you trying to accomplish?
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Calling Jscrict

Post by spacebiscuit »

Sorry guys I meant to say "if". So I am calling javacript depending on an if statement.

So for example:

Code: Select all

<? 
if(!$_POST[id]){
?>
<script type="text/javascript">
DO SOMETHING
</script>
<?
}
?>
My question is how do I trigger the javascript, because in most other cases it seems to be triggered by events such a mouseclick.

Does the question make better sense now?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Calling Jscrict

Post by califdon »

No, I'm afraid not. That can't work, because PHP is running in the server, before anything is sent to the browser, so if it was your intent that some Javascript should be sent conditionally to the browser, you would need to use echo or print to send it to the browser. If you had in mind somehow to execute the Javascript, that's not going to happen in the server.

In other words, PHP cannot ever "call" Javascript, or vice versa. They are never executing at the same time or in the same computer.

Why don't you explain just what you are trying to do, then maybe we can help you understand how to do it.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Calling Jscrict

Post by spacebiscuit »

Here is exactly what I am trying to do.

If a PHP session variable is not set I want to prompt the user for their affiliate ID via a javascript popup screen. I have the popup working and returning the variable to the PHP. What I can't figure out is how to trigger the javascript (when the session variable has not yet been set).

Thanks.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Calling Jscrict

Post by califdon »

spacebiscuit wrote:Here is exactly what I am trying to do.

If a PHP session variable is not set I want to prompt the user for their affiliate ID via a javascript popup screen. I have the popup working and returning the variable to the PHP. What I can't figure out is how to trigger the javascript (when the session variable has not yet been set).

Thanks.
I don't know what you are referring to when you say "a javascript popup screen." If you are depending on Javascript to do something, you must understand that Javascript runs in the browser, so there is no more PHP involved. You cannot ever detect PHP session variables in Javascript because they are running on different computers at different points in time. You need to be more specific about what you are trying accomplish. Never mind the programming talk, tell us what you want to happen, in terms of what the user experiences.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Calling Jscrict

Post by spacebiscuit »

Apologies, I'm not sure how I can put it any more clearly:

If a PHP variable is not set I want to present a popup which is written in Javacript which captures an id from the user which is returned to the php.

I have this working via the modal class (?) but that is triggered when a link is clicked. I want the javascript to be triggered when the PHP variable is not set (see above).

I have made some progress... for example I can set the value of a javascript variable depending on the value of a php as follows:

Code: Select all

var affid = <?php echo isset($_POST['affid']) ? 1 : 0 ?>;
I'm then doing the following:

Code: Select all

$(document).ready(function () {

    if (affid==0) {
		// scroll to top
		$('html, body').animate({scrollTop:0}, 'fast');

		// before showing the modal window, reset the form incase of previous use.
		$('.success, .error').hide();
		$('form#contactForm').show();
		
		// Reset all the default values in the form fields
		$('#affid').val('Input your affiliate ID');

		//show the mask and contact divs
		$('#mask').show().fadeTo('', 0.7);
		$('div#contact').fadeIn();

	}

});
the above code has been modified from an onclick example that works... the original code was in an external .js file as follows:

Code: Select all

$(function() {

	// load the modal window
	$('a.modal').click(function(){

		// scroll to top
		$('html, body').animate({scrollTop:0}, 'fast');

		// before showing the modal window, reset the form incase of previous use.
		$('.success, .error').hide();
		$('form#contactForm').show();
		
		// Reset all the default values in the form fields
		$('#affid').val('Input your affiliate ID');

		//show the mask and contact divs
		$('#mask').show().fadeTo('', 0.7);
		$('div#contact').fadeIn();

		// stop the modal link from doing its default action
		return false;
	});
What I think I've done is to remove the onclick listener but I guess I am still missing something as my revised version does not behave as expected.

Thank you.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Calling Jscrict

Post by califdon »

We might get closer to providing you some help if you would drop all the code and programming references and simply describe what you want to achieve, strictly from the user's point of view. It's not at all clear to me what it is you want to accomplish.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Calling Jscrict

Post by spacebiscuit »

Ok habng on I think I have it working, I stripped out my script to the bear basics to post in a reply and it now is behaving as expected. I'm just piecing the origianl script back together step-by-step to see what was causing the conflict.

I'll report back shortly!
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Calling Jscrict

Post by spacebiscuit »

Ok, I've been hammering away at his for over 6 hours and I've finally figured it out.

So this is what I am doing - In my PHP script I am using this to catch the value of a php variable:

Code: Select all

var affid = <?php echo isset($_POST['affid']) ? 1 : 0 ?>;
Then I am linking to an external .js file which contains the following:

Code: Select all

$(function() {
	
	if(affid==0){	
                // load the modal window
		DO SOMETHING
	}

	// close the modal window if close div or mask div are clicked.
	$('div#close, div#mask').click(function() {
		$('div#contact, div#mask').stop().fadeOut('slow');
	});

	// when the Submit button is clicked...
	$('input#submit').click(function() {

	     $('.error').hide();
	     $('.success').slideDown('slow');

	});

});
So if the PHP variable $affid is not set a javacript popup appears and prompts for a user id. When this is input and submitted the value is returned to the original php script which is exactly what I wanted to happen.

Something I would like to ask though, you can see from my javascript that when the form is submitted there is a success message that slides down:

Code: Select all

$('input#submit').click(function() {

     $('.error').hide();
     $('.success').slideDown('slow');

});
This works well but the popup closes as soon as the messages has slid down, is there a way of delaying the window closing allowing the user to read the success message. Alternatively the form could wait for the user to click the close icon (div#close) on the popup?

Thanks for letting me think aloud and figure this out - jscript has been a steep learning curve so far!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Calling Jscrict

Post by califdon »

Before I forget, avoid saying "jscript"--that's Microsoft's version of a client-side scripting language, entirely different from Javascript (technically, it should be called ECMAscript). And what you're dealing with there is actually jQuery, an add-on Javascript library, so it's not plain Javascript. I'm not fully qualified with jQuery, so I don't immediately see why your popup is closing. You need to research what those jQuery calls really do.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Calling Jscrict

Post by spacebiscuit »

Ah ok I thought jscript wasjust shorthand for javascript... I'm learning fast!

So Jquery is a library of javascript with pre-written functionality is that correct? How does that differ from Ajax, is Ajax simply another library and therefore ias it just a matter of preference in choosing between the two.

Many thamks.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Calling Jscrict

Post by califdon »

spacebiscuit wrote:Ah ok I thought jscript wasjust shorthand for javascript... I'm learning fast!

So Jquery is a library of javascript with pre-written functionality is that correct? How does that differ from Ajax, is Ajax simply another library and therefore ias it just a matter of preference in choosing between the two.

Many thamks.
Yes, jQuery is a library; that is, it is a collection of functions written in Javascript to perform specialized functions, including a comprehensive way to address HTML elements within a web page, and perform fancy functions such as fading, sliding, etc. It is very useful, but it is not a part of standard Javascript. To use jQuery you need to provide a link to the jQuery source, which you can either download and install on your server, or just refer to a standard repository, such as provided by Google.

AJAX is not a library, it isn't even a language, it is merely the term used to describe using a built-in Javascript object named XMLHttpRequest. You can learn about XMLHttpRequest and AJAX at http://www.w3schools.com/xml/xml_http.asp

In summary, PHP is a scripting language that is executed in the web server, Javascript is a scripting language (quite different from both Jscript and Java, which is a compiled programming language) that is executed in the browser, HTTP is a markup language and CSS is an extension of HTTP (at least, that's my best description of it). Many "libraries" (collections of functions) exist for Javascript (see http://javascriptlibraries.com/).
Post Reply