Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy how to insert php code inside javascritp

    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

  • #2
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.

    Code:
    <?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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •