Click to See Complete Forum and Search --> : How can I use javascript variables in PHP?


zw1971
09-20-2003, 05:42 PM
I have a list box to be defined by php like this:

<?php
do {
?>
<option value="<?php echo $row_document_type['dt_id']?>"<?php if (!(strcmp($row_document_type['dt_id'], $row_document_type['dt_description']))) {echo "SELECTED";}?> ><?php echo $row_document_type['dt_description']; ?></option>
<?php
$dt_id=$row_document_type['dt_id'];
mysql_select_db($database_localhost, $localhost);
$query_document_index_definitions = "SELECT * FROM t_documentindexdefinition WHERE dt_id = $dt_id ORDER BY did_id ASC";
$document_index_definitions = mysql_query($query_document_index_definitions, $localhost) or die(mysql_error());
$row_document_index_definitions = mysql_fetch_assoc($document_index_definitions);
$totalRows_document_index_definitions = mysql_num_rows($document_index_definitions);
$dt_id=$row_document_type['dt_id'];
echo "<script language='javascript'>";
echo "var document_index_definition$dt_id=new Array(";
do{
echo "'";
echo $row_document_index_definitions['did_description'];
echo "'";
echo ",";
}while ($row_document_index_definitions=mysql_fetch_assoc($document_index_definitions));
echo "'foo')";
echo "</script>";
?>
<?php
} while ($row_document_type = mysql_fetch_assoc($document_type));


And I have dynamica html to show up a table based on the listbox selection like this:

<script language="JavaScript">
function display_index(obj)
{

var document_index=eval('document_index_definition'+obj)
//delete all rows
if ( tblDocumentIndexType.rows.length > 1 )
{
rowCnt = tblDocumentIndexType.rows.length ;
for ( rowPos = 1 ; rowPos < rowCnt ; rowPos++ )
{
tblDocumentIndexType.deleteRow( 1 ) ;
}
}


for(i=0;i<(document_index.length-1);i++)
{
var newRow=tblDocumentIndexType.insertRow();
var newCell=newRow.insertCell();
newCell.insertAdjacentHTML("AfterBegin", "<tr><td><font face=\"Times New Roman, Times, serif\" size=\"2\"><strong>&nbsp;&nbsp;&nbsp;" + document_index[i] + "<\/strong><\/font></td>");
newCell.insertAdjacentHTML("AfterBegin", "<td><input name=\"+did[i]+\" type=\"text\" size=\"15\"></td></tr>");
}

var newRow=tblDocumentIndexType.insertRow();
var newCell=newRow.insertCell();
newCell.insertAdjacentHTML("AfterBegin", "<tr><td></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"Submit\" name=\"Submit\" value=\"Submit\"></td></tr>");

}
</script>

question is after I click the "submit' button, how can I read the input boxes in the dynamic html in PHP.

Hope I explained my question clearly. Thanks for your help

S1L3NC3
09-20-2003, 05:49 PM
Umm, dont know if i can help, but heres part of a post from pyro earlier showing how to pass variables from PHP to Javascript:

<?PHP
$var = "test";
?>

<script type="text/javascript">
var = <?PHP echo $var; ?>;
alert (var);
</script>
The javascript will alert you the value "test" when it is run.

zw1971
09-20-2003, 06:00 PM
thanks S1L3NC3 ,

but i'm trying to use javascript variables from php, instead reverse.

Thanks anyway.

Khalid Ali
09-20-2003, 06:15 PM
Originally posted by S1L3NC3
Umm, dont know if i can help

Why post if you don't know...???

the only way to pass variable values to server side is using the HTTP requests there is no other ways( ofcourse if you use Java applet then you can get way with it)

S1L3NC3
09-20-2003, 06:17 PM
Im sorry, but theres no use in being rude about it. Just figured id try. Not all of us are uber coders such as yourself.

My appologies

pyro
09-20-2003, 06:21 PM
As Khalid said, when moving variables from JavaScript to PHP, it requires you to reload the page. There are two basic ways of sending data to a PHP page.

1: Use the query string, and parse the data off in the PHP. Take this, for example:

somepage.php?var1=test&var2=test2

You can then use this in PHP to get the values:

$var1 = $_GET['var1']; # will equal test
$var2 = $_GET['var2']; # will equal test2

2: You can submit a form, and use either $_GET or $_POST (depending on what method you used with your form) in the same manner as above (with the indexes being the name of the form elements).

AdamBrill
09-20-2003, 06:22 PM
What exactly are you trying to do? While you can't just use JavaScript variables in PHP, I'm sure there is a solution to your problem... Just post a description of what you are trying to do and I'll try to give you some pointers. :)

Khalid Ali
09-20-2003, 06:32 PM
Originally posted by S1L3NC3
but theres no use in being rude about it. Just figured id try

If you recomend a solution that is exactly the opposite of a the required answer,I think that constitutes some thinking.
And I was not rude,I merely stated the fact that do no guide them wrong if you can not point to the correct direction.

zw1971
09-20-2003, 07:09 PM
Originally posted by AdamBrill
What exactly are you trying to do? While you can't just use JavaScript variables in PHP, I'm sure there is a solution to your problem... Just post a description of what you are trying to do and I'll try to give you some pointers. :)

I tried to explain it in my first post, but it seems it's not clear. I have an array defined by javascript like this:
echo "<script language='javascript'>";
echo "var document_index_definition$dt_id=new Array(";
do{
echo "'";
echo $row_document_index_definitions['did_description'];
echo "'";
echo ",";
}while ($row_document_index_definitions=mysql_fetch_assoc($document_index_definitions));
echo "'foo')";
echo "</script>";

Then I'll have a form which contains text fields named by the array above. Then how can I refer to the form elements value in php?

AdamBrill
09-20-2003, 07:46 PM
Ok... You already have the variables in PHP, right? You are writing them into the JS... So, you should be able to put all of the values from the JS array into a PHP array(when you are echoing them out, just include a line to add them into a PHP array), and then you will have them for use later on in the page. If you don't know what I mean, let me know. ;)

zw1971
09-21-2003, 12:14 AM
So my code should be like:

echo "<script language='javascript'>";
echo "var document_index_definition$dt_id=new Array(";
//php array
echo $document_index_definition.$dt_id=new Array(";
do{
echo "'";
echo $row_document_index_definitions['did_description']
;
echo "'";
echo ",";

//php array
echo $row_document_index_definitions['did_description'],

}while ($row_document_index_definitions=mysql_fetch_assoc
($document_index_definitions));
echo "'foo')";
echo "</script>";

//php array
echo "foo)";


Right? I don't have access to my work box, will try that tomorrow. thanks for your backup. I'll probably ask you some other questions tomorrow. I'm desperately working on a project which is due Monday.
:( :(

AdamBrill
09-21-2003, 09:15 AM
Umm... That's not what I meant. It should be more like this(untested):

echo "<script language='javascript'>";
$php_array = Array();
$php_array_count = 0;
echo "var document_index_definition$dt_id=new Array(";
//php array
//echo $document_index_definition.$dt_id=new Array(";
do{
echo "'";
echo $row_document_index_definitions['did_description'];
echo "'";
echo ",";

//php array
$php_array[$php_array_count] = $row_document_index_definitions['did_description'];
$php_array_count++;

}while ($row_document_index_definitions=mysql_fetch_assoc

($document_index_definitions));
echo "'foo')";
echo "</script>";

//php array
echo "foo)";