1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Javascript variable/value transfer

Discussion in 'PHP' started by kaeesh, May 23, 2005.

  1. #1
    How i can send a value to php variable to javascript variable?

    How i can send a value to javascript variable to php variable?

    Can you show me a simple example.
     
    kaeesh, May 23, 2005 IP
  2. palespyder

    palespyder Psycho Ninja

    Messages:
    1,254
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    168
    #2
    okay, the first one is easy

    
    <script>
    
    var whatever = "<?= $phpVar ?>";
    
    </script>
    
    Code (markup):
    The PHP will be parsed and the value of $phpVar will become the value of var whatever. The second one is not possible without refreshing the page because PHP is parsed server side and Javascript is parsed client side. The PHP is always parsed "before" the page is shown to the client in other words.
     
    palespyder, May 23, 2005 IP
  3. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Make sure you encode phpVar properly. For example, if phpVar contains a double quote, you'll end up with a broken JS assignment (not to mention opening up a security hole if phpVar is formed using some form of user's input).

    You can use get/post arguments or window.location. For example, in your JS program set some input value and then in your PHP script retrieve it and assign a PHP variable.

    JS:
    document.getElementById("i1").value = jsVar;

    HTML:
    <form method="GET" action="action.php">
    <input id="i1" name="i1" value="">
    ...
    </form>

    PHP:
    $phpVar = $_GET["i1"]

    J.D.
     
    J.D., May 23, 2005 IP
  4. kaeesh

    kaeesh Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks a lot
     
    kaeesh, May 23, 2005 IP
  5. cdburgess

    cdburgess Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This is interesting. But the $_GET doesn't work if I establish a variable inside a javascript. For example:

    <script type="text/javascript" language="javascript">
    var A = 5;
    document.write('<sc'+'ript type="text/javascript" src="http'+ (s_prot?'s':'') +'://somedomain.com/my.php" /></sc'+'ript>');
    </script>

    Now in the my.php that sits on somedomain.com I run some javascript inline but I also want to use the A variable that is being passed, inside the PHP. How can I pass the A (which in this case is = to 5) into a PHP variable? Can this even be done?
     
    cdburgess, Aug 23, 2005 IP
  6. cdburgess

    cdburgess Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I think I discovered one option that I will post here until someone has another or says it cannot be done... it's an improved $_GET['A'] method. If I simply tack the variable onto the end of the my.php like this:

    ...my.php?A=5

    Then I can simply call $_GET['A'] and I will get the variable. But I am still interested in knowing if there is a way to get the information as I mentioned in the previous post.
     
    cdburgess, Aug 23, 2005 IP
  7. rickbkis

    rickbkis Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ajax/XMLHTTP seems to be the developing way to do this. The php version is xajax. It's still pretty embryonic, but you can check out more here:
    www.sourceforge.net/projects/xajax.

    rickb
     
    rickbkis, Aug 25, 2005 IP
  8. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This code will never work, never mind passing variables back in forth - it's simply invalid HTML/JS. Think about it, when you are done with document.write, your result will be

    <script type="text/javascript" language="javascript">
    var A = 5;
    <script type="text/javascript" src="http: //somedomain.com/my.php"/>
    </script>
    </script>

    As for the variable A above, JS and PHP are not two parts of a magic programming environment that will exchange variables automatically. You have to write some code to make it happen. See my example for one way of handling this. There are also other ways two.

    J.D.
     
    J.D., Aug 25, 2005 IP
  9. cdburgess

    cdburgess Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9

    Hmmm... that is interesting... then why is it working? :confused:
     
    cdburgess, Aug 25, 2005 IP
  10. dweebsonduty

    dweebsonduty Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    Digital Goods:
    1
    #10
    I like to use cookies to do it.
     
    dweebsonduty, Sep 15, 2009 IP
  11. jammedejaldo

    jammedejaldo Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hallo, ik heb een javascript wat ik wil mee mailen naar mijn email adres. het script wat hieronder staat is wat de klant heeft besteld.
    (Inhoud van de winkelwagen).

    </script>
    		<form action="mailer.php" method="post" name="basket">
    	    <script>try{dwH('tbl',1);}catch(e){location.reload();}</script>
    			<table cellspacing=0 cellpadding=3 border="0" sf:object="ViewbasketMaster" class="ViewbasketMaster" width="90%" align="center">
    				<!-- ' -->
    				<script>dwH();dwH('header',1)</script>
    				<tr sf:object="ViewbasketHeader" class="GC4 ViewbasketHeader">
    					<td colspan="2" class="GC5 ViewbasketHeader">LD.LD_TITLE</td>
    					<td class="GC5 ViewbasketHeader">LD.LD_PRICE</td>
    					<td class="GC5 ViewbasketHeader">LD.LD_QUANTITY</td>
    					<td class="GC5 ViewbasketHeader"><D_></D_><img src="../media/trans.gif" height="1" width="1"></td>
    					<td class="GC5 ViewbasketHeader">LD.LD_SUBTOTAL</td>
    					<td class="GC5 ViewbasketHeader"><img src="../media/trans.gif" height="1" width="1"></td>
    				</tr>
    
    				<!-- ' -->
    				<script>dwH();dwH('row1',1)</script>
    				<tr sf:object="ViewbasketRow1" class="GC14 ViewbasketRow1">
    					<td><I_><img width=45 height=45 src="../media/sample.gif"></I_>&nbsp;</td>
    					<td align=left><a href=javascript:go("[IDX]")><T_>ShopFactory</T_></a></td>
    					<td align=right><nobr><P_>$ 379.00</P_></nobr></td>
    					<td align=right>
    					  <div align="center">
    					    <input name="_[IDX]" type="text" class="GC26" value="1" size="4" />
    				      </div>
    				    <nobr></nobr></td>
    					<td align=right><D_></D_>&nbsp;</td>
    					<td align=right><nobr><S_>$ 379.00</S_></nobr></td>
    					<td align=right>&nbsp;</td>
    				</tr>
    				<!-- ' -->
    				<script>dwH();dwH('frow1',1)</script>
    				<tr sf:object="ViewbasketRow1" class="GC14 ViewbasketRow1">
    					<td><I_><img width=25 height=25 src="../media/sample.gif"></I_>&nbsp;</td>
    					<td align=left><T_>ShopFactory</T_></td>
    					<td align=right>LD.LD_FREE&nbsp;</td>
    					<td><Q_>
    					  <div align="center">1</div>
    					</Q_>&nbsp;</td>
    					<td colspan=3>&nbsp;</td>
    				</tr>
    				<!-- ' -->
    				<script>dwH();dwH('opt1',1)</script>
    				<tr sf:object="ViewbasketRow1" class="GC14 ViewbasketRow1">
    					<td>&nbsp;</td>
    					<td align=left><T_> + Extra Option</T_></td>
    					<td align=right><nobr><P_> $ 0.00</P_></nobr></td>
    					<td colspan="4" >&nbsp;</td>
    				</tr>
    				<!-- ' -->
    				<script>dwH();dwH('row2',1)</script>
    				<tr sf:object="ViewbasketRow2" class="GC15 ViewbasketRow2">
    					<td class="Visibility"><I_><img width=45 height=45 src="../media/sample.gif"></I_>&nbsp;</td>
    					<td align=left><a href=javascript:go("[IDX]")><T_>ShopFactory</T_></a></td>
    					<td align=right><nobr><P_>$ 379.00</P_></nobr></td>
    					<td align=right>
    					  <div align="center">
    					    <input size="4" name="_[IDX]2" value="1" class="GC26" />
    					  </div>
    				    <nobr></nobr></td>
    					<td align=right><D_></D_>&nbsp;</td>
    					<td align=right><nobr><S_>$ 389.95</S_></nobr></td>
    					<td align=right>&nbsp;</td>
    				</tr>
    				<!-- ' -->
    				<script>dwH();dwH('frow2',1)</script>
    				<tr sf:object="ViewbasketRow2" class="GC15 ViewbasketRow2">
    					<td><I_><img width=25 height=25 src="../media/sample.gif"></I_>&nbsp;</td>
    					<td align=left><T_>ShopFactory</T_></td>
    					<td align=right>LD.LD_FREE</td>
    					<td><Q_>
    					  <div align="center">1</div>
    					</Q_></td>
    					<td colspan=3>&nbsp;</td>
    				</tr>
    				<!-- ' -->
    				<script>dwH();dwH('opt2',1)</script>
    				<tr sf:object="ViewbasketRow2" class="GC15 ViewbasketRow2">
    					<td>&nbsp;</td>
    					<td align=left><T_> + Extra Option</T_></td>
    					<td align=right><nobr><P_> $ 0.00</P_></nobr></td>
    					<td colspan="4" >&nbsp;</td>
    				</tr>
    				<!-- ' -->
    				<script>dwH();dwH('prods',1)</script>
    				<tr sf:object="ViewbasketExtras" class="GC16 ViewbasketExtras">
    				  <td colspan="6" align="right">LD.LD_PRODUCTS:</td>
    				  <td align="right"><nobr>
    				    <TL>$ 768.95</TL>
    				    </nobr></td>
    				</tr>
    				<!-- ' -->
    				<script>dwH();dwH('prods_dsc',1)</script>
    				<tr sf:object="ViewbasketExtras" class="GC16 ViewbasketExtras">
    				  <td colspan="6" align="right">LD.LD_DISCOUNTED_TO:</td>
    				  <td align="right"><nobr>
    				    <TLD>$ 730.50</TLD>
    				    </nobr></td>
    				</tr>
    				<!-- ' -->
    				<script>dwH();dwH('shp',1)</script>
    				<tr sf:object="ViewbasketExtras" class="GC16 ViewbasketExtras">
    				  <td colspan="6" align="right">LD.LDTOTALSHIPPING:</td>
    				  <td align="right"><nobr>
    				    <TS>$ 5.00</TS>
    				    </nobr></td>
    				</tr>
    				<!-- ' -->
    				<script>dwH();dwH('ttl',1)</script>
    				<tr sf:object="ViewbasketExtras" class="GC16 ViewbasketExtras">
    				  <td colspan="6" align="right">LD.LD_TOTAL:</td>
    				  <td align="right"><nobr>
    				    <TL>$ 735.50</TL>
    				    </nobr></td>
    				</tr>
    
    				<tr >
    					<td colspan=10 align=left><img src="../media/trans.gif" height="1" width="1"></td>
    				</tr>
    							<!-- ' -->
    				<script>dwH();dwH('chkbx',1)</script>
    			</table>
                <!-- ' -->
    				<script>dwH();dwH('ckout2',1)</script>
                  <!-- ' -->
    	            <script>dwH();dwH('ckout1',1)</script>
                  <!-- ' -->
                    <script>dwH()</script>
    				<script>
    				var dfb=d.basket;
    				if(bsk){
    					var css=core.css;
    					if(css)css.init(d);
    					if(bsk.items.cnt()==0)dw(LD.LD_NO_ITEMS_IN_BASKET);
    					else list_items(d.basket);
    					core.rl_total();
    				} else if (dfb) {
    				dw(chop(dfb.ckout1));
    				dw(chop(dfb.tbl));
    				dw(chop(dfb.header));
    				
    				dw(chop(dfb.row1));
    				dw(chop(dfb.opt1));
    				dw(chop(dfb.row2 ));
    				dw(chop(dfb.opt2));
    				dw(chop(dfb.shp));
    				dw(chop(dfb.ttl));
    				dw(chop(dfb.chkbx));
    				dw(chop(dfb.ckout2));
    				}
    				
    				</script>
                    <input type="submit" value="Submit" name="submit">
    		  </form>
    Code (markup):
    Deze code is al verwerkt om te kunnen versturen. In het begin staat
    <form action="mailer.php" method="post" name="basket">
    Code (markup):
    en op het einde staat
    </form>
    Code (markup):
    Ik weet niet of het in de mailer.php zit of hierboven.
    Dit is de mailer.php

    <?php 
    if(isset($_POST['submit'])) { 
    $to = "info@sansoft.200u.com"; 
    $subject = "Form Tutorial"; 
    $name_field = $_POST['name']; 
    $email_field = $_POST['email']; 
    $message = $_POST['message']; 
      
    $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message"; 
      
    echo "Data has been submitted to $to!"; 
    mail($to, $subject, $body); 
    } else { 
    echo "blarg!"; 
    } 
    ?>
    Code (markup):
    Alvast bedankt!!

    (Het mailen werkt al wel) alleen hij mailt alleen de naam,subject en email. Verder niet dat scriptje.
    En dat script wil ik mee laten sturen.. dat is de bestelling van de klant. ( wat hij heeft besteld ).
     
    jammedejaldo, Nov 27, 2009 IP
  12. madhaviadepu

    madhaviadepu Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    thanks a lot
     
    madhaviadepu, Dec 17, 2009 IP
  13. madhaviadepu

    madhaviadepu Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    ..............................................
     
    madhaviadepu, Dec 17, 2009 IP
  14. madhaviadepu

    madhaviadepu Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    .............................................
     
    madhaviadepu, Dec 17, 2009 IP
  15. madhaviadepu

    madhaviadepu Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    ......................................................
     
    madhaviadepu, Dec 17, 2009 IP
  16. AustinQPT

    AustinQPT Member

    Messages:
    75
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #16
    At this point in time the only way to do this is through AJAX. Send a javascript variable to a php page through AJAX, then the php ajax page will use the $_GET['info'] function to get the javascript variable. Then do whatever you want with that new information. This is very good to use when saving info to a database without having to reload. It is also god for making games. Such as this example one and this example one.
     
    AustinQPT, Dec 18, 2009 IP
  17. eketahuna

    eketahuna Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    This works fine, if what you need is an form entry box with the required text inside it. On top of that my text is HTML-formatted.

    I can get what I want by using .innerHTML to a <div id= > but for some reason this string is not returned back to the client.

    Normally this is all I need to do <? echo $myDBdata;?>
    where $myDBdata could be formatted-HTML, plain text, or json-like delimited fields.

    Client-side I always use xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); which may be the problem here?

    My Twitter-query javascript works fine client-side without the Ajax but I wanted to avoid client-side bloat.

    Any suggestions?
     
    eketahuna, Jan 23, 2010 IP
  18. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Thanks for resurrecting a 5 year old thread :rolleyes:
     
    JAY6390, Jan 23, 2010 IP
  19. eketahuna

    eketahuna Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Great comment JAY!
    I moved it all back client-side where it works properly with marginal extra code :)
     
    eketahuna, Jan 23, 2010 IP
  20. prosta

    prosta Peon

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    The PHP will be parsed and the value of $phpVar will become the value of var whatever. The second one is not possible without refreshing the page because PHP is parsed server side and Javascript is parsed client side. The PHP is always parsed "before" the page is shown to the client in other words.
     
    prosta, Jan 29, 2010 IP