我如何在javascript标记中编写php代码

时间:2019-05-17 07:45:35

标签: javascript php jquery

我在一些javascript函数标签中编写了php代码。 php代码将从html代码中调用。请检查我的代码。

$("#insert-form").append(
    "<div class='form-group row'>" +
    "<div class='col-sm-7'>" +
    "<select id='listBox2' class='form-control'>" +
    "<option selected></option>" +
    alert("<?php foreach($pekerjaan->result_array() as $tampilpekerjaan){ ?>"); +
    "<option value='" +
    alert("<?php echo $tampilpekerjaan['kode_pekerjaan']; ?>");  +
    "'>&nbsp;&nbsp;" +
    alert("<?php echo $tampilpekerjaan['nama_pekerjaan']; ?>"); +
    "</option>" +
    alert("<?php } ?>"); +
    "</select>" +
    "</div>" +
    "<div class='col-sm-2'>" +
    "<input type='text' name='volume[]' class='form-control' placeholder='Vol'>" +
    "</div>" +
    "<div class='col-sm-3'>" +
    "<input type='text' name='harga[]' class='form-control' placeholder='Harga'>" +
    "</div>" +
    "</div>");

  $("#jumlah-form").val(nextform); // Ubah value textbox jumlah-form dengan variabel nextform
});

我的问题是php代码不起作用

alert("<?php foreach($pekerjaan->result_array() as $tampilpekerjaan){ ?>"); +
"<option value='" +
alert("<?php echo $tampilpekerjaan['kode_pekerjaan']; ?>");  +
"'>&nbsp;&nbsp;" +
alert("<?php echo $tampilpekerjaan['nama_pekerjaan']; ?>"); +
"</option>" +
alert("<?php } ?>"); +

2 个答案:

答案 0 :(得分:0)

有解决方案!

要获得可读的代码,请在 HTML代码foreach() php变量,并使用Jquery 获取此HTLM 代码并附加到#insert-form 元素

通过这种方式,您可以获得可读的代码

完整的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div class="form-to-append" style="display: none">
        <div class='form-group row'>
            <div class='col-sm-7'>
                <select id='listBox2' class='form-control'>
                    <option selected></option>
                    <?php foreach($pekerjaan->result_array() as $tampilpekerjaan) { ?>
                        <option value='<?= $tampilpekerjaan['kode_pekerjaan'] ?>'><?= $tampilpekerjaan['nama_pekerjaan'] ?></option>
                    <?php } ?>
                </select>
            </div>
            <div class='col-sm-2'>
                <input type='text' name='volume[]' class='form-control' placeholder='Vol'>
            </div>
            <div class='col-sm-3'>
                <input type='text' name='harga[]' class='form-control' placeholder='Harga'>
            </div>
        </div>
    </div>

    <div id="insert-form"></div>

    <script src="//code.jquery.com/jquery-3.4.1.min.js"></script>
    <script>
        $(function() {
            // Previous CODE

            var new_form_html = $('.form-to-append').html();

            $("#insert-form").append(new_form_html);

            // $("#jumlah-form").val(nextform);
        })
    </script>
</body>
</html>

答案 1 :(得分:0)

通常,像这样完成此逻辑的构建。

    var pekerjaan = 'xx';
    jQuery.ajax({
        url: "/jumlah-form/pekerjaan.php",
        type: "post",
        dataType: "text",
        async: true,
        data: "pekerjaan="+pekerjaan,
        success: function(data){
            console.log(2);
        }