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.
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.
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.
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?
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.
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
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.
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_> </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_> </td> <td align=right><nobr><S_>$ 379.00</S_></nobr></td> <td align=right> </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_> </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> </td> </tr> <!-- ' --> <script>dwH();dwH('opt1',1)</script> <tr sf:object="ViewbasketRow1" class="GC14 ViewbasketRow1"> <td> </td> <td align=left><T_> + Extra Option</T_></td> <td align=right><nobr><P_> $ 0.00</P_></nobr></td> <td colspan="4" > </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_> </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_> </td> <td align=right><nobr><S_>$ 389.95</S_></nobr></td> <td align=right> </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_> </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> </td> </tr> <!-- ' --> <script>dwH();dwH('opt2',1)</script> <tr sf:object="ViewbasketRow2" class="GC15 ViewbasketRow2"> <td> </td> <td align=left><T_> + Extra Option</T_></td> <td align=right><nobr><P_> $ 0.00</P_></nobr></td> <td colspan="4" > </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 ).
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.
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?
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.