hi! i just want to know, is it possible to pass the value of a variable coming from javascript to php?? knowing that the variable is inside a function??

function (id,ind)
 {
    var clicked_row = id;
}

i want to pass the value of clicked_row to php..

thanks in advance ^_^

Recommended Answers

All 7 Replies

I could be wrong but with JS being client side and PHP being server side, there is no real way to pass that variable (with the exception of using $_GET and page load).

Example:

<script language="JavaScript">
var clicked_row = id;
location.href="SomePage.php?id=" + clicked_row;
</script>

// GET the variable from url

<?PHP
$clicked_row = $_GET['id'];
?>

You could also use AJAX to pass it to a PHP file via $_GET

I could be wrong but with JS being client side and PHP being server side, there is no real way to pass that variable (with the exception of using $_GET and page load).

Example:

<script language="JavaScript">
var clicked_row = id;
location.href="SomePage.php?id=" + clicked_row;
</script>

// GET the variable from url

<?PHP
$clicked_row = $_GET['id'];
?>

for additional only i just want to add to the post of CFROG.

makesure you have the SomePage.php
in SomePage.php you put this code
the variable "id" will be passed on that page.
so you have to Get it.

<?PHP
$clicked_row = $_GET['id'];
echo $clicked_row;
?>

ANOTHER EXAMPLE

<script type="text/javascript">

width = screen.width;
height = screen.height;

if (width > 0 && height >0) {
    window.location.href = "http://localhost/main.php?width=" + width + "&height=" + height;
} else 
    exit();

</script>

main.php

<?php
echo "<h1>Screen Resolution:</h1>";
echo "Width  : ".$_GET['width']."<br>";
echo "Height : ".$_GET['height']."<br>";
?>

oh, thanks for the code it really helped..however, whenever i click that specific row, the tendency is that the page reloads..is it possible for the page not to reload anymore? because if thats the case, i think its another problem..please help. tnx,

PS: i merged all the codes in one php page only..

hi! i just want to know, is it possible to pass the value of a variable coming from javascript to php?? knowing that the variable is inside a function??

function (id,ind)
 {
    var clicked_row = id;
}

i want to pass the value of clicked_row to php..

thanks in advance ^_^

use JSON
http://php.net/manual/en/book.json.php

With the code example given the page would have to reload in order to grab the variable from the url. I'm not overly familiar with it but you might want to look into JRM's advice and experiment with JSON.

okei here it goes, the scenario is like this: i have a datagrid "dhtmlxGrid", what i want is that, when the user selects a certain row, the program will get the value of the selected row, now i can get the value already, however, it is in javascript format..

<table width="235" border="1"cellspacing="0" cellpadding = "0" id = "parameter_table">
<tr>
<th width="85"><font face="MS Sans Serif" size="1"><strong>Parameter Seq</strong></font></th>
<th width="150"><font face="MS Sans Serif" size="1"><strong>Parameter Name</strong></font></th>
</tr>
<?php	
$resultParam_pp = mysql_query("SELECT * FROM SPC_InfoParameter ") or die(mysql_error());	
while($row = mysql_fetch_assoc($resultParam_pp))
{		
$lblParameter_pp = $row['ParameterName'];
$lblParameterSeq_pp = $row['Parameter_seq'];	
					
echo "<tr id = '$lblParameterSeq_pp'>";
echo "<td><font face = 'MS Sans Serif' size = 1>";
echo $lblParameterSeq_pp;
echo "</font></td>";
echo "<td><font face = 'MS Sans Serif' size = 1>";
echo $lblParameter_pp;
echo "</font></td>";
echo "</tr>";	
}
?>
</table>
<script>
var grid = dhtmlXGridFromTable("parameter_table");	
grid.setStyle("background-color:#CCCC99;color:black; font-weight:bold;", "","color:green;", "");
 grid.attachEvent
 	("onRowSelect", 
		 function(id,ind)
		 {
		 	var clicked_row = id;
		}
 	);

  </script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.