?
Solved

Can I change the value of a PHP variable using Javascript?

Posted on 2007-07-25
15
Medium Priority
?
1,622 Views
Last Modified: 2008-03-03
Can I change the value of a php variable using javascript?
For example I have a few onmouseOver events that I want to change the value of a PHP variable $spec

onmouseOver='bigimage.src="<?php echo $imagebig1; ?>";<?php $spec = 1; ?>'
onmouseOver='bigimage.src="<?php echo $imagebig2; ?>";<?php $spec = 2; ?>'
onmouseOver='bigimage.src="<?php echo $imagebig3; ?>";<?php $spec = 3; ?>'

and then my Buy Button has the following
onClick="location.href='javascript:AddItem(<?php echo $rs_prods['PID']?>,1,<?php echo $spec; ?>)'"

function AddItem($PID, $qty, $spec)
      {
            var item = $PID;
            var qty = $qty;
            var spec = $spec;
            
            alert(spec);
            self.location = "addtocart.php?action=add_item&id="+item+"&qty="+qty+"&spec="+spec;
            }

I doesnt appear to be working as it just sets $spec equal to 3 every time

Thanks.
0
Comment
Question by:sabecs
  • 4
  • 4
  • 3
  • +2
15 Comments
 
LVL 17

Expert Comment

by:psimation
ID: 19564395
It's not javaScript that's changeing $spec to 3, it's your PHP code...

onmouseOver='bigimage.src="<?php echo $imagebig1; ?>";<?php $spec = 1; ?>'
onmouseOver='bigimage.src="<?php echo $imagebig2; ?>";<?php $spec = 2; ?>'
onmouseOver='bigimage.src="<?php echo $imagebig3; ?>";<?php $spec = 3; ?>'

and in particular the last parts: <?php $spec = 3; ?>'

this particular peice of code does exactly what you say: it assigns the value of "3" to $spec.

I am suspecting that you actually only want to "echo" the value of $spec, and not actually set the value?


0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19564479
you're mixing javascript (client) and php (server) code, which would not do what you expect.
Hence the answer to your question:
> Can I change the value of a PHP variable using Javascript?
is no (except you mean server-side javascript).

If you client-side javascript changes a value, a HTTP request has to be send to your (server-side) php code.
0
 

Author Comment

by:sabecs
ID: 19564758
Thanks for your feddback , but is there a way that $spec can be set based on the javascript onMouseover event?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:psimation
ID: 19564799
No

When your page code loads; PHP has first "dibs" on it. It will look for any PHP tags and do whatever is inside it, and replace it with the output in HTML. JavaScript on the otherhand is not executed on the server, it's inside your code, lying "dormant" untill a user's browser opens it, then the JS gets loaded onto their browser and the code is executed without the "PHP" knowing about it.

The only way to make JS change the variables is to send data back to the server.

Having said all that, you *may* find some clever bloke has come op with a AJAX method to do just this...
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19564861
I guess you should think about what you want to achieve.
Why would you change a php variable's value? does that produce another/new page? If not, you just need to tweak your javascript code.
0
 
LVL 17

Accepted Solution

by:
psimation earned 2000 total points
ID: 19564870
PS, why do you want PHp to have to deal with this $spec variable? It seems that all you are trying to do is determine over which image a user has hovered before they click the Buy button - and then you send that variable back as a $_GET var anyway. Rather have JavaScript register the variable value on mouseover and the simply pass it to the onclick string?

I am rather interested to know why you want to have the values set on mouseover if you have a separate buy button. What happens if the user accidentally moves his mouse ober another image on his way to the buy button?


0
 
LVL 4

Expert Comment

by:Lo-Tan
ID: 19565233
LOL, a rather funny way to change the price perhaps (sorry that this isn't an intuitive solution)

Please stop mixing Javascript and PHP, they aren't a hand in hand language.  They are evaluated at completely different times.  This entire method is going to be replaced with hardcoded values instead of variables

function AddItem($PID, $qty, $spec)
      {
            var item = $PID;
            var qty = $qty;
            var spec = $spec;
           
            alert(spec);
            self.location = "addtocart.php?action=add_item&id="+item+"&qty="+qty+"&spec="+spec;
            }

will become something like this and be useless (php first, then your javascript client code will call this wonderful method)
function AddItem(117, 1, 3)
      {
            var item = 117;
            var qty = 1;
            var spec = 3;
            // etc...
            }

If you want to do something like this, have a form with hidden variables:

<form method="post" action="yourPHPcartOrderConfirmPageSure.php">
  <input type="hidden" id="spec" name="spec" value="3"/>
  <input type="submit" value="Complete Order"/>
</form>

// then on one of your mouseoverrrrrrrs
<img src="myimg.gif" onmouseover="javascript: document.getElementById('spec').value='4';"/>

// Then on your PHP page that receives the post request (im not real fluent with PHP)
$spec = $_POST["spec"];

I don't know if this is exactly what you want, but I hope it leads you in the right direction.
0
 
LVL 2

Expert Comment

by:Edanisko
ID: 19566948
Check out JSON, zXML and AJAX to find out more about mixing javascript and serverside php.
0
 

Author Comment

by:sabecs
ID: 19570729
Thanks for your feed back,
Could I then set the Javascript spec variable with the onMouseover
      onMouseOver='bigimage.src="<?php echo $imagebig1; ?>"; var spec = 1;'
and then my Buy Button has the following
      onClick="location.href='javascript:AddItem(<?php echo $rs_prods['PID']?>,1,spec)'"

function AddItem($PID, $qty, spec)
      {
            var item = $PID;
            var qty = $qty;
            var spec = spec;
           
            alert(spec);
            self.location = "addtocart.php?action=add_item&id="+item+"&qty="+qty+"&spec="+spec;
            }
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19572350
you're still mixing client and server code
Please don't do that, or explain why it it is done this way.
0
 
LVL 4

Expert Comment

by:Lo-Tan
ID: 19573953
Its like shooting arrows in the dark... why do you need PHP to process things, is it pulling item specific data from the database before you use your additem method?  If so, use AJAX to make a call to the server with the appropriate spec value and have a php page either write out the values you need, or have that php page simply echo the AddItem javascript method.  Then you can keep updating that javascript method on the client (the AddItem method) with the one that gets wrote back to you from the php page.
0
 
LVL 4

Expert Comment

by:Lo-Tan
ID: 19573959
I have an AJAX library that I wrote and will send you free of charge if you want, and in the end if you take the time to learn why PHP/JavaScript is not working in sync and also spend the time to learn AJAX, you will have a solution ready to roll by this weekend
0
 

Author Comment

by:sabecs
ID: 19574828
Thanks for your help, in the end you where correct, I did not need the php variable I just set the Javascript variable as follows.

var spec = "1";
function AddItem($PID, $qty, spec){      
               var item = $PID;
             var qty = $qty;
            var spec = spec;

            self.location = "addtocart.php?action=add_item&id="+item+"&qty="+qty+"&spec="+spec;
            }


onmouseOver='bigimage.src="<?php echo $imagebig1; ?>";spec = "1";'>
onmouseOver='bigimage.src="<?php echo $imagebig2; ?>";spec = "2";'>
onmouseOver='bigimage.src="<?php echo $imagebig3; ?>";spec = "3";'>


onClick="location.href='javascript:AddItem(<?php echo $row_view_products['PID']?>,1,spec)'"



Now for my explanation as to what I was trying to achieve,  I have 4 thumbnail images of the same product in different colours.  When a customer mouses over the thumbnail the main larger image is replaced with that particular color. If the customer does not select a colour from a select list next to the buy button then the color they are viewing is automatically picked and added to their shopping cart.

It could probably be done much better than this but I am still a newbie, and this works for me.

Thanks
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19578049
> .. did not need the php variable I just set the Javascript variable as follows
hmm, your code is still strange, 'cause $PID and $qty is not valid javascript syntax (in your example)
0
 
LVL 4

Expert Comment

by:Lo-Tan
ID: 19578163
LOL yeah I have no idea how that's working either :(
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question