...

View Full Version : how to insert php code inside javascritp



GiorgioTO
12-18-2006, 04:03 PM
Hello,
I've this problem: i've to populate 3 combo box with data that come from 3 different tables in a MySql database, i'm trying to do that using a javascript in the following way but noghing go like i need...any idea ?

<script language="JavaScript">
var i_macro = <?php echo $num_rows; ?>;
document.write("Numero righe Macro: " + i_macro);
var macro_id = new array();
var macro_descrizione = new array();
<?php
for($i=0; $i < $num_rows; $i++)
{
$row = mysql_fetch_array($result);
$macro_id = $row["id_problema"];
echo "<br>".$macro_id."<br>";
$macro_descrizione = $row['tipo_problema'];
echo $macro_descrizione;
?>
macro_id[<?php $i ?>] = <?php $macro_id ?>;
document.write("Macro_id: " + macro_id[<?php $i ?>]);
macro_descrizione[<?php echo $i; ?>"] = <?php $macro_descrizione; ?>;
document.write("Numero righe Macro: " + macro_descrizione[<?php $i; ?>]);
<?php
}
?>
</SCRIPT>

Thanks :thumbsup:

Ancora
12-18-2006, 04:22 PM
Georgio:

An example. The $options array is the array returned by $row = mysql_fetch_array($result);

FYI: JavaScript is case-sensitive. new array() is wrong, new Array() is correct.



<?php

$options = array ('One', 'Two', 'Three', 'Four', 'Five');

$fillSelect = '';
foreach($options as $value)
{
$fillSelect .= "<OPTION VALUE=\"$value\">$value</OPTION>";
}

?>

<html>
<head>
<script type="text/javascript">

function showChoice(nValue){

alert(nValue);
}

</script>
</head>
<body>
<form>
<select name="anyName" onchange="showChoice(this.value)">
<option value=""> Make a Selection </option>
<?php echo $fillSelect; ?>
</select>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum