Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
165
rated 0 times [  170] [ 5]  / answers: 1 / hits: 21600  / 11 Years ago, sun, august 25, 2013, 12:00:00

I am trying to pass a php value, obtained from a join query, to a javascript function. The javascript function will open a new window and show some data based on the passed value.My query works fine but the php value is not passed to the JS function.



My code :



<script type=text/javascript>

function product(var) {
window.open( view_product_info.php?var, myWindow,
status = 1, height = 300, width = 300, resizable = 0 )
}
</script>

\ the line where i am trying to pass the php varibale

echo '<td align=center ><a href=javascript:product('.$product_id.');> <br/> '.$row['product_name'].'</a></td>';


why the php value $product_id is not passed to the product function.



Thanks in advance.



The code:



     <script type=text/javascript>
<!--
function product(var) {
window.open( view_product_info.php?id, myWindow,
status = 1, height = 300, width = 300, resizable = 0 )
}
function company() {
window.open( index.php, myWindow,
status = 1, height = 300, width = 300, resizable = 0 )
}
function category() {
window.open( index.php, myWindow,
status = 1, height = 300, width = 300, resizable = 0 )
}


//-->



      <?php include(includes/header.php); 


$search = $_POST['search'];
$sql= my query1..;

$sql2= my query2;

$result=mysql_query($sql);
$result2=mysql_query($sql2);
if($result) {
echo '<center>';
echo '<table cellpadding=0 cellspacing=0 border=1 width=100%>';
echo '<tr><th>Sr No.</th><th>Product Name</th><th>Company Name</th> <th>Category</th></tr>';
$number=1;
while ($row = mysql_fetch_array($result)){
$row2 = mysql_fetch_array($result2);
echo $product_id=$row2[product_id];

echo '<tr> ';
echo '<td align=center >'.$number.'</td>';


echo '<td align=center ><a href=javascript:product('<?= $product_id?>')>



''';



            echo '<td align=center><a href=javascript:company() ><br/>  '.$row['company_name'].'</td>';
echo '<td align=center><a href=javascript:category() ><br/> '.$row['category_name'].'</td>';

$number=$number+1;

}echo '</tr>';
echo'</table>';
echo'</center>';

}
else {
echo No data found;
//echo mysql_error();

}
}
}
?>

More From » php

 Answers
1

If it's not a number, you need to quote it:



<?php
echo '<td align=center ><a href=javascript:product(''.$product_id.'');>
<br/> '.$row['product_name'].'</a></td>';
?>


Or, a neater way, use php just when needed (no PHP tags around, it's HTML with inserted PHP):



<td align=center ><a href=javascript:product('<?= $product_id ?>')> 
<br/><?= $row['product_name'] ?></a></td>


You can also define a JavaScript value and assign the PHP value to it and then use it, like:



var pid = '<?= $product_id ?>'; // then call product(pid)
etc...


EDIT



Code fix.



This:



<?php
...
// php stuff
...

echo '<tr> ';
echo '<td align=center >'.$number.'</td>';

echo '<td align=center ><a href=javascript:product('<?= $product_id?>')>
<br/>'<?= $row['product_name']?>'</a></td>';

echo '<td align=center><a href=javascript:company() ><br/> '.$row['company_name'].'</td>';
echo '<td align=center><a href=javascript:category() ><br/> '.$row['category_name'].'</td>';

$number=$number+1;

}echo '</tr>';
echo'</table>';
echo'</center>';


Can become something like this:



<?php
...
// php stuff
...

?> // close the PHP tag and switch to HTML
<tr>
<td align=center ><?= $number ?></td>
<td align=center ><a href=javascript:product('<?= $product_id?>')> <br/>'<?= $row['product_name']?>'</a></td>

<td align=center><a href=javascript:company() ><br/> <?= $row['company_name'] ?></td>
<td align=center><a href=javascript:category() ><br/> <?= $row['category_name'] ?></td>

<?php // reopen PHP tag when needed
$number++; // incrementation simplified
}
?> // close again
</tr>
</table>
</center>


Something like that.



Also, read here about the deprecated mysql_* functions and why you should switch to mysqli_* or PDO.


[#76147] Friday, August 23, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shylaelisan

Total Points: 37
Total Questions: 94
Total Answers: 110

Location: Angola
Member since Tue, May 5, 2020
4 Years ago
;