PDA

View Full Version : Send a java variable to php


arne2
05-12-2007, 04:02 PM
Hi,
i want to sent a java variable to php and i don't know how to do this. Could anyone maybe help me.

Here's the problem:
I have a form where people can insert text.
The form tag is like this:
<form name="forname" action="mycode.php" method="post" onsubmit="return submitForm();">

The submitform function is the following:
function submitForm() {

updateRTEs();

rte1 = htmlDecode(document.RTEDemo.rte1.value);

alert(rte1);
}

It now alerts back what you enter, that's working. But what i now want to do is put it in a database (using php). So i need to send the rte1 variable to php. How can i do this best? I don't mind needing redirection or something...

THANK YOU VERY MUCH!
Arne

rafiki
05-12-2007, 04:15 PM
<form name="forname" action="mycode.php?rte1=+rte1+" method="post" onsubmit="return submitForm();">
you get the javascript to add the variable rte1 to the mycode.php action
then use $_GET['rte1']; on the next page
im not good with javascript but that is how it would be done i guess

arne2
05-12-2007, 04:56 PM
i don't think that will work as the rte1 variable only exists after submitting, so after the <form> tag is already printed. Thanks for your help anyway! Fortunately i found a solution myself!

rafiki
05-12-2007, 07:01 PM
what was the solution?

arne2
05-12-2007, 07:40 PM
Hi rafiki!
I first of all want to thank you as i've seen you are a very helpful member of codingforums as I seen a lot of helpful posts from you. If i can ever help you in any way, I certainly will!

The solution now:
rte1 = htmlDecode(document.RTEDemo.rte1.value);
document.write('<b><u>Gegevens controleren</u></b>');
document.write('<br>Wijzigende pagina: <?php echo"$page";?>');
document.write('<br> HTML code:<form name="secondform" action="putindb.php?chpage=<?php echo"$page";?>" method="post"><textarea rows="30" cols="80" name="htmlcode">');
document.write(rte1);
document.write('</textarea>');
document.write('<p><input type="submit" name="submit" value="Opslaan" /></p>');
So basically i just made a form with the thing in and i didn't pass it as that was to difficult. Anyway, if anyone knows a way, i would greatly appreciate it because it would be easier.
While searching google i found a possible solution that tried the following:
<? $var="?><script language=javascript>document.write(jsvar)</script><?";?> but that doesn't work, does it?

arne2
05-12-2007, 07:42 PM
I was also thinking if this is possible, haven't tested it:

<?php
header(location:http://www.blabla.com/?var=?><script>document.write(var)</script><?);?> but i guess not, don't know (syntaxes might be wrong, but you get the idea)

rafiki
05-12-2007, 07:48 PM
no that wouldnt work as PHP wouldve been executed before javascript has a chance to write the var in the header function, if you head over to the javascript forum they may help you draw up a function that will add the var to the form action when submitted so you could use it how i posted first

Thanks for the recognition i do try to help as much as i can, and learn while im at it, if it wasnt for codingforums i would have never learned any PHP

Ultragames
05-14-2007, 09:37 AM
rafiki'a method is the best way to go. Here is what I suggest:

Instead of having a type="submit" button, make it a type="button" button, that runs your submitForm() onClick. Change the action of the form like rafiki suggested, and then run a submit() function on the form. This adds only 1 line of JS to your original function.