dtxooq1020 2015-06-09 10:03
浏览 41

在PHP中定义JS函数

I am Working on a little Webpage, which has a .php-include hierarchy. Now I want to generate two relying Dropdown-Menues, where the second one is updated, after you choose an option in the first one.

I would prefer if the code for this page could stay in one file, as I want to achieve this without splitting the code.

The code looks something like this:

<?php
echo '<div id="dashheader">
<form action="..." method="get" enctype="text/plain">
<p id="dashheadline">Selbstkosten ermitteln</p>
..
<td><select name="lieferant" onChange="javascript:updateArtikel(this.value);" >
...
<div id="art">
    <select name="artikel">
    <option value="">Erst Lieferanten wählen</option>
    </select>
</div>';
?>

Now i have the Javascript which I want to be executed on a Change in the "lieferant"-Dropdown(Simple Ajax Call):

<script type="text/javascript">
    function updateArtikel(str) {
        ...
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("art").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","cure1.php?q="+str,true);
        xmlhttp.send();
        ...
     }
</script>

cure1.php is the same .php-File I generate the Output in. It also contains additional php-Code, in Case it is called with a parameter "q" (see AJAX-Call):

if(isset($_GET['q'])){
    echo '<select id="art" name="artikel">';
    $abfrage = //MYSQL-Query; 
    $ergebnis = mysqli_query($con,$abfrage);
    while($row = mysqli_fetch_object($ergebnis))
    {
        echo '<option value = "'.$row->PositionsNr.'" > '.$row->Artikelbezeichnung.'</option>';
    }
    echo '</select>';
}

Now the Question: Is it really possible to leave this code in one File? If not, How shall I put the code, if the "cure1.php" (my page) is loaded per "index.php?menu=10" ?

  • 写回答

1条回答 默认 最新

  • dtio35880438 2015-06-09 10:15
    关注

    see your hierarchy of php file carefully. At last code should like

    <?php 
    ......
    //php code
    ?>
    <html>
        <head>
            <script>
              //javascript code
            </script>
        </head>
    </html>
    

    you may also echo all html code within php tags. This is standard, you may not restricted to do same.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法