Jump to content

Pass Variable from php TO javascript


cfgcjm

Recommended Posts

ok, this is my predicament. I need to grab a the variable '$user[$j][2]' from a php script and insert it into javascript (or maybe another php document...i think). What has to happen is that when the variable is returned it will put it's value in the text field "un". Below is my current code...In the page change.php when a user changes the value of the combobox (name) it runs the script alteration.php which verifies if a user is a registered user or unregistered user and returns that value via echo to the div tag "checked" on the page. At the same time as the echo value is returned is when i need to pass the '$user[$j][2]' variable...somehow...to the change.php page in the 'un' text field. I hope that makes sense, here is the code of the two files:

 

change.php

<?php
$session = mysql_connect("localhost", "xxxx", "xxxx");
mysql_select_db("xxxx");
$query = "select * from users";

$result = mysql_query($query);
if ( !$result )
{
echo ( "<P>Error doing a select: " . mysql_error() . "</P>" );
}
else
{
// succesful read - how many records ?

$rr=mysql_num_rows($result); //this is calculating the number of rows read from database and puts it into variable rr

// loop over rows, counts the total number and prints them
$user = array(array()); // define array students
for ($i=0; $i<$rr; $i++) {
    $nextresult = mysql_fetch_array($result);
$fname = $nextresult['first_name'];
$lname = $nextresult['last_name'];
$journal = $nextresult['client'];
$un = $nextresult['username'];
$username="$lname, $fname";
$user[$i] [0] = $username;
$user[$i] [1] = $journal;
$user[$i] [2] = $un;
} 
}
mysql_close($session);
sort($user);
?>
<html>
<head>
<script language="javascript" type="text/javascript">
function research() {
var url = "alteration.php?param=";
var name = document.getElementById("name").value;
http.open("GET", url + escape(name), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}

function handleHttpResponse() {
if (http.readyState == 4) {
results = http.responseText;
/* Again, we're assuming your username input ID is "username" */
var name = document.getElementById("name").value;
/* If the username is available, Print this message: */
document.getElementById('checked').innerHTML = results;
}
}
function getHTTPObject() {
var xmlhttp;
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject();


</script>
</head>
<body>
<form action="amend.php" method="post" style="width: 600px">
<select name="name" id="name" onChange="research();">
<option></option>
<?php
for ($j=0; $j<$i; $j++) {
echo "<option>{$user[$j][0]}</option>";
}
?>
</select><input name="un" id="un" value="" type="text"><div name="checked" id="checked">
</div>

<fieldset name="clientgroup" style="width: 105px">
Yes<input name="Yes" type="radio" id="yes"  value="1">
No<input name="Yes" type="radio" id="yes" value="0">
</fieldset>

<input name="Submit1" type="submit" value="submit"></form> 
</body>
</html>

 

alteration.php

<?php
$host   = 'localhost'; // your host name
$dbuser = 'xxx'; // your database username
$dbpass = 'xxx'; // your database user password
$dbname = 'xxx'; // your database name
$query = "select * from users";

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname);

$result = mysql_query($query);
if ( !$result )
{
echo ( "<P>Error doing a select: " . mysql_error() . "</P>" );
}
else
{
// succesful read - how many records ?
$rr=mysql_num_rows($result); //this is calculating the number of rows read from database and puts it into variable rr
// loop over rows, counts the total number and prints them
$user = array(array()); // define array students
for ($i=0; $i<$rr; $i++) {
    $nextresult = mysql_fetch_array($result);
$fname = $nextresult['first_name'];
$lname = $nextresult['last_name'];
$journal = $nextresult['client'];
$un = $nextresult['username'];
$username="$lname, $fname";
$user[$i] [0] = $username;
$user[$i] [1] = $journal;
$user[$i] [2] = $un;
} 
$name = addslashes($_GET['param']);
for ($j=0; $j<$i; $j++) 
{
	if($name == $user[$j][0]) 
	{ $client=$user[$j][1];
 	  if($client==1)
 	  	{echo "Registered Client";
		 echo $user[$j][2];}
 	  if($client==0)
 	  	{echo "Unregistered Client";
		 echo $user[$j][2];}

	}

}
}
mysql_close($conn);
?>

Link to comment
https://forums.phpfreaks.com/topic/87378-pass-variable-from-php-to-javascript/
Share on other sites

didn't work...change.php doesn't have access to the variable $user[$j][2] from alteration.php

 

I did this:

function handleHttpResponse() {
if (http.readyState == 4) {
results = http.responseText;
un='<?echo $user[$j][2]?>';
alert(un);
/* Again, we're assuming your username input ID is "username" */
var name = document.getElementById("name").value;
/* If the username is available, Print this message: */
document.getElementById('checked').innerHTML = results;
}
}

 

Alert box was blank

he's using ajax to call the php.

 

put what I wrote in the output of alteration.

 

if that doesn't work.

 

make add the following to the output of alteration.

 

echo '<form id="rs" style="display:none;">';

echo "<input type=hidden name=fun value='$user[$j][2]'>";

echo '</form>';

 

then in javascript

un=document.all.rs.fun.value;

 

 

I'm very confused by what you want me to do...this is not valid php...you can't put a script inside of php with another php inside of that

 

	for ($j=0; $j<$i; $j++) 
{
	if($name == $user[$j][0]) 
	{ $client=$user[$j][1];
 	  if($client==1)
 	  	{echo "Registered Client";
		<script>
		un='<? echo $user[$j][2]?>';
		</script>
            if($client==0)
 	  	{echo "Unregistered Client";
		<script>
		un='<? echo $user[$j][2]?>';
		</script>		}

}

 

It doesn't matter if he is using AJAX or not. If you want to access the same variable in two different PHP script, you should use a $_SESSION variable to pass it between them. I do that all the time using AJAX techniques.

 

Ken

I'm trying to do this session variable stuff but now my change.php page is getting a

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/digiconm/public_html/Homeplate/portal/change.php:1) in /home/digiconm/public_html/Homeplate/portal/change.php on line 2

 

This is the current code there are NO spaces before <?php or session_start():

<?php
session_start();
$session = mysql_connect("localhost", "xxx", "xxx");
mysql_select_db("xxx");
$query = "select * from users";

$result = mysql_query($query);
if ( !$result )
{
echo ( "<P>Error doing a select: " . mysql_error() . "</P>" );
}
else
{
// succesful read - how many records ?

$rr=mysql_num_rows($result); //this is calculating the number of rows read from database and puts it into variable rr

// loop over rows, counts the total number and prints them
$user = array(array()); // define array students
for ($i=0; $i<$rr; $i++) {
    $nextresult = mysql_fetch_array($result);
$fname = $nextresult['first_name'];
$lname = $nextresult['last_name'];
$journal = $nextresult['client'];
$un = $nextresult['username'];
$username="$lname, $fname";
$user[$i] [0] = $username;
$user[$i] [1] = $journal;
$user[$i] [2] = $un;
} 
}

mysql_close($session);
sort($user);
?>
<html>
<head>
<script language="javascript" type="text/javascript">
function research() {
var url = "alteration.php?param=";
var name = document.getElementById("name").value;
http.open("GET", url + escape(name), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}

function handleHttpResponse() {
if (http.readyState == 4) {
results = http.responseText;
un='<?php $_SESSION['unc'];?>';
alert(un);
/* Again, we're assuming your username input ID is "username" */
var name = document.getElementById("name").value;
/* If the username is available, Print this message: */
document.getElementById('checked').innerHTML = results;
}
}
function getHTTPObject() {
var xmlhttp;
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject();


</script>
</head>
<body>
<form action="amend.php" method="post" style="width: 600px">
<select name="name" id="name" onChange="research();">
<option></option>
<?php
for ($j=0; $j<$i; $j++) {
echo "<option>{$user[$j][0]}</option>";
}
?>
</select><input name="un" id="un" value="" type="text"><div name="checked" id="checked">
</div>

<fieldset name="clientgroup" style="width: 105px">
Yes<input name="Yes" type="radio" id="yes"  value="1">
No<input name="Yes" type="radio" id="yes" value="0">
</fieldset>

<input name="Submit1" type="submit" value="submit"></form> 
</body>
</html>

I'm very confused by what you want me to do...this is not valid php...you can't put a script inside of php with another php inside of that

 

	for ($j=0; $j<$i; $j++) 
{
	if($name == $user[$j][0]) 
	{ $client=$user[$j][1];
 	  if($client==1)
 	  	{echo "Registered Client";
		echo"<script>\n";
		echo"un='$user[$j][2]';\n"
		echo"</script>\n";}
            if($client==0)
 	  	{echo "Unregistered Client";
		echo"<script>\n";
		echo"un='$user[$j][2]';\n"
		echo"</script>\n";}

}

 

I did this:

	for ($j=0; $j<$i; $j++) 
{
	if($name == $user[$j][0]) 
	{ $client=$user[$j][1];
 	  if($client==1)
 	  	{echo "Registered Client";
		echo"<script>\n";
		echo"un='$user[$j][2]';\n";
		echo"</script>\n";
		echo "<input type=text name=fun value='$user[$j][2]'>";
		}
            if($client==0)
 	  	{echo "Unregistered Client";
		echo"<script>\n";
		echo"un='$user[$j][2]';\n";
		echo"</script>\n";
		echo "<input type=text name=fun value='$user[$j][2]'>";
		}

	}

 

1. i dont want it in a new form

2. i made them visible so i could see the result

            Problem: the Text Box displays "Array[2]"

 

 

I'm not sure why

I shortened it up to this and it's working so far:

	for ($j=0; $j<$i; $j++) 
{
	if($name == $user[$j][0]) 
	{ $client=$user[$j][1];
	  $un=$user[$j][2];
 	  if($client==1)
 	  	{echo "Registered Client";
		 echo "<input type='hidden' name='un' id='un' value='$un'>";
		}
            if($client==0)
 	  	{echo "Unregistered Client";
		 echo "<input type='hidden' name='un' id='un'  value='$un'>";
		}

	}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.