Jump to content

[SOLVED] echo problem passing values to a function. Please help.


sayedsohail

Recommended Posts

You have the string delimited by single quotes. Variables are not expanded inside single quotes.

 

Try this:

<?php
echo '<tr id="action" title="Click to display sites below" onclick="passvar(' . $pageNum . ',' . $id . ',' . $name . ',this );">';
?>

 

Ken

Here is the code, which

 

echo passes four values to the function and the function in turn just alert the values.

 

<?php

echo "<tr onclick=\"passvar(${pageNum}, ${id}, ${name}, this );\">";

echo "<td>$id</td<td>$name</td></tr>";

?>

<SCRIPT>

here is my javascript function.

function passvar(pan,rec,nam,obj)

{

 

var bage = pan;

var cid = rec;

var cname = nam;

alert("This is your company name"+cname);

}

</SCRIPT>

Ah :)

 

Check your Javascript console to make sure you're not sending:

 

passvar(1, 3, name ,this);

 

Because in that name is a constant!  Try:

 

<?php
echo "<tr id=\"action\" title=\"Click to display sites below\" onclick=\"passvar(${pageNum}, ${id}, '${name}', this );\">";
?>

 

:) thanks!

 

<ins>By the way, in that case the error you would get is: Variable 'name' is undefined in ... on line ...</ins>

Million thanks, now the alert works, but when i tried to remove alert and redirect the values to a new page it doesn't. here is my modified code.

 

<?php

echo "<tr onclick=\"passvar(${pageNum}, ${id}, ${name}, this );\">";

echo "<td>$id</td<td>$name</td></tr>";

?>

<SCRIPT>

here is my javascript function.

function passvar(pan,rec,nam,obj)

{

 

var bage = pan;

var cid = rec;

var cname = nam;

//alert("This is your company name"+cname);

document.location.href="sitedetail.php?page="+bage+"&cid="+cid"&cname="+cname;  // the problem is here, since i can't retrieve cname value using $name1 = $_GET['cname'];

}

</SCRIPT>

Me bad, it works fine now, but when i remove the alert and tries to redirects to new page with all the values its giving error, here is my modified code.

 

echo "<tr onclick=\"passvar(${pageNum}, ${id}, '${name}', this );\">";

 

 

 

function passvar(pan,rec,nam,obj)

{

 

var bage = pan;

var cid = rec;

var cname = nam;

//alert("This is your company name"+cname);

 

document.location.href="sitedetail.php?page="+bage+"&cid="+cid"&cname="+cname; // the problem lies here now.

 

}

 

 

 

 

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.