-4


I have tried to call simple javascript function in php. I can't call that function showResult().I don't know what is wrong with this code. Can you please check for me? I appreciate your help.
http://www.mediafire.com/?hycdzegki1n79lg
With regards,

Updated: Yes. I knew that it is reparative question (also that one is now closed with negative ) but I think problem is different. Actually, I can write other similar to this just that I am stuck for this one. I can't post well here for whole or necessary function because after I post, it become messy. I am sorry for making trouble to you. What link shall I upload?

<?php
echo '<select name="companyNameList" onchange="showResult()" >';
for ($row = 0; $row < 2; $row++)
{
echo '<option value="'.$placement[$row]["company_name"].'">'.$placement[$row]    ["company_name"].'</option>';
}
echo '</select>';
?>
Khant Thu Linn
  • 5,905
  • 7
  • 52
  • 120

2 Answers2

0

There are couple of issues.

HTML structure and Form tags are not proper, they should be in body

<html>
   <head>
      <script>
      </script>
   </head>
   <body>
     <form>
     </form>
   </body>
</html>

this is the structure you should follow

They way you are writing your onChange function is correct, but the issue in my opinion is in the showResult JS function

in the function you have used

<script type="text/javascript">
function showResult()
{
    var companyName = companyNameList.value;
    var insertvalue = d.getElementById('lblCompanyName');
    insertvalue.innerHTML = companyName
    //    document.getElementById("lblCompanyName").value=document.getElementById("companyNameList").value;
        <?php echo "hi";?>

}
</script>

please note that <?PHP echo 'hi'; ?> will only out put 'hi' in the JS which means nothing and may cause JS errors. you should use it like this

<script type="text/javascript">
function showResult()
{
    var companyName = companyNameList.value;
    var insertvalue = d.getElementById('lblCompanyName');
    insertvalue.innerHTML = companyName
    //    document.getElementById("lblCompanyName").value=document.getElementById("companyNameList").value;
        <?php echo "alert('hi');";?>

}
</script>

I hope these will solve your issues

EDIT

Give an id to your dropdown select companyNameList.

<script type="text/javascript">
    function showResult()
    {

        <?php echo "alert('hi');";?>
        var companyName = document.getElementById("companyNameList");
        var companyNameValue = companyName.options[companyName.selectedIndex].value;
        var insertvalue = document.getElementById('lblCompanyName');

        insertvalue.innerHTML = companyNameValue;
    }
</script>

Please install firebug in your firefox to catch any errors in script

EDIT COMPLETE CODE

Check this code

<?php
$link = mysqli_connect('localhost', 'root', '','xerox_test');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
$statement = "SELECT * from form_results";
$result = mysqli_query($link,$statement);
$placement= array(); 
$i=0;
while($row = mysqli_fetch_array($result))
{    
    $rowktl[$i] = $row;
    $placement[$i]['company_name'] = $rowktl[$i]['company_name'];
    $i++;
   }
?>
<html>

<head>
<script type="text/javascript">
    function showResult()
    {

        <?php echo "alert('hi');";?>
        var companyName = document.getElementById("companyNameList");
        var companyNameValue = companyName.options[companyName.selectedIndex].value;
        var insertvalue = document.getElementById('lblCompanyName');

        alert(companyNameValue);
        insertvalue.innerHTML = companyNameValue;
    }
</script>
</head>
<body>
<form method="post" action="">  
<p>Select the data</p> 
<?php
echo '<select name="companyNameList" id="companyNameList" onchange="showResult()" >';
for ($row = 0; $row < 2; $row++)
{
    echo '<option value="'.$placement[$row]["company_name"].'">'.$placement[$row]["company_name"].'</option>';
}
echo '</select>';
?>
<br>
<input name="Submit" type="button" class="Submit" value="Submit"  onClick="showResult()" />
<table border=2>
    <tr>
        <td>Case Number <br></td>
        <td>Technican ID <br></td>
        <td>Company Name <br><label id="lblCompanyName">Here</label> </td>
    </tr>
    <tr>
        <td>Model Number <br></td>
        <td>Machine Serial Number<br></td>
        <td>Company Address<br></td>
    </tr>
    <tr>
        <td>Customer Name<br><?php echo json_encode($placement[0]['customer_name']);?></td>
        <td>Customer Email<br></td>
        <td>Contact Number<br></td>
    </tr>
    <tr><td colspan=3>Remark<br></td></tr>
</table>
</form>
</body>
</html>

I have added the id="companyNameList" to <select> in php code above the loop. Also updated the JS function.

Now if you use this, can you see the selected value in alert? If you can not see that it means we are not yet getting the selected value. If you see the proper value, it means the assignment is not correct.

Aamir Mahmood
  • 2,704
  • 3
  • 27
  • 47
0

The javascript isn't the problem, I made a simple js-fiddle for you, I think the problem is your php code, and what it generates. Could you post the full html that the php code renders please?

I think $placement[$row]['company_name'] might not contain a string.

Here the jsfiddle showing you the js. http://jsfiddle.net/HWFQH/1/

Otherwise try this, it might be a bit easier to read for you, if it's in the view / html code of yours.

<select name="companyNameList" onchange="showResult()">
<?php for ($row = 0; $row < 2; $row++):?>
     <option value="<?php echo $placement[$row]["company_name"];?>">
          <?php echo $placement[$row]["company_name"];?>
      </option>
<?php endfor;?>
</select>
Stefan Konno
  • 1,337
  • 2
  • 16
  • 28
  • Thanks for your help.. I can see the required text in the drop down list (will update to label). So, I think showResult() is not working. I want to post here but I think not enough space. May I know how I can pass to you? – Khant Thu Linn Feb 19 '13 at 09:16
  • @KhantThuLinn: Edit your initial question with all the necessary information and source code. – oktopus Feb 19 '13 at 09:35