Jump to content

[SOLVED] Refresh page and pass a value across


noobstar

Recommended Posts

Hi what im tring to do is basically this:

 

I have 2 radio buttons if one is clicked it goes to one function and is supposed to set a variable to a value. If the other radio button is clicked it goes to another similar function and is supposed to set the same variable to something else though.

 

This works only for one but not the other for some reason, below is the code. Thank you for any help in adv.

 

Javascript:

<SCRIPT LANGUAGE='JavaScript'>
<!-- Begin
redirTime = '2000';
redirURL = 'http://www.test.test.com/shout.php';
function artpickOut() { <?php $artpick = "Outside Article"; ?> self.setTimeout('self.location.href = redirURL;',redirTime); }
function artpickMy() { <?php $artpick = "My Article" ?> self.setTimeout('self.location.href = redirURL;',redirTime); }
// End -->
</script>

 

My radio buttons:

<input type="radio" name="optArtPick" value="Outside Article" onClick="artpickOut()">Outside Article
<input type="radio" name="optArtPick" value="My Article" onClick="artpickMy()">My Article

 

 

thanks again!

Link to comment
Share on other sites

You're getting confused in the ways PHP and Javascript are processed.

PHP (PHP: Hypertext Preprocessor) is a server side language. That means that all of that your code is executed and outputted before the file is sent to the client.

Javascript is a client side language, meaning that the code is executed at the client's side, hence it's executed after the file is sent, after any server-side processing takes place.

 

In this context, PHP executes all of the PHP code first, and then sends it off:

Try putting this line at the end of your code:

<?=$artpic?> or <?php echo $artpic?>

If you notice, it'll always output the last thing you set it to, "My Article". That's simply the way the code is executed.

 

Try using GET variables (url query).

Example: (you don't really need javascript, or radio inputs for that matter)

<a href='test.php?var=outside'>Outside Article</a><br>
<a href='test.php?var=my'>My Article</a>

test.php:

<?php
$var=$_GET['var']; //This gets the variable from the query
if ($var=='outside') echo "Outside article";
else if ($var=='my') echo "My article";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.