Jump to content

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


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.

 

}

 

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.