dozrhldy285751 2013-02-19 08:38
浏览 34
已采纳

javascript与php无法正常工作[关闭]


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>';
?>
  • 写回答

2条回答 默认 最新

  • dounai9592 2013-02-19 08:46
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错