简体   繁体   English

我如何将PHP代码附加到Javascript附加函数下<script> tag

[英]How do i append the PHP code to the Javascript append function under <script> tag

My Script file is 我的脚本文件是

<script type="text/javascript">
   $(document).ready(function(){
          $(".select2").select2();
          var MaxInputs       = 50; //maximum input boxes allowed
          var InputsWrapper   = $("#overall_wrapper"); //Input boxes wrapper ID
          var AddButton       = $("#AddMoreFileBox"); //Add button ID
          var x = InputsWrapper.length; //initlal text box count
          var FieldCount=1; //to keep track of text box added      
          $(AddButton).click(function (e)  //on add input button click
          {
           if(x <= MaxInputs) //max input box allowed
           {
           FieldCount++; //text box added increment
           //add input box
           $(InputsWrapper).append('<div class="repeat_element" id="row_'+FieldCount+'"><div class="col-sm-1" id="row1_'+FieldCount+'"><input type="text" class="form-control" name="s_no[]" value="'+FieldCount+'"  /></div><div class="col-sm-2" id="row1_'+FieldCount+'"><select class="form-control select2" name="part_no[]" id="part_no" onchange="choose_parts(this.value);" required><option value="">Please Select</option></select></div><div class="col-sm-3" id="row1_'+FieldCount+'"><input type="text" class="form-control" name="part_desc[]" value=""  /></div><div class="col-sm-1" id="row1_'+FieldCount+'"><input type="text" class="form-control" name="part_mrp[]" value=""  /></div><div class="col-sm-1" id="row1_'+FieldCount+'"><input type="text" class="form-control" name="req_qty[]" value=""  /></div><div class="col-sm-1" id="row1_'+FieldCount+'"><input type="text" class="form-control" name="instock[]" value=""  /></div><div class="col-sm-2" id="row1_'+FieldCount+'"><select class="form-control" name="billing_type[]"><option value="Cash">Cash</option><option value="Credit">Credit</option></select></div><div class="col-sm-1" id="row1_'+FieldCount+'"><div onclick="hiding('+FieldCount+')" class="removeclass"><div class="btn btn-danger" id="AddMoreFileBox"><i class=" fa fa-minus-circle" title="Delete"></i> </div></div></div>');
                    $(".select2").select2();  // Reinitialize the select2 again when the div is appended
                    x++; //text box increment
                    }
                    return false;
                    });

        });
</script>

And i have a page where i call this Add button jquery and i get the second row fine but i am unable to call the PHP functions like foreach , if into the append script. 而且我有一个页面,我将其称为“添加”按钮jquery,我得到了第二行,但if进入追加脚本, if无法调用foreach类的PHP函数。

在此处输入图片说明

My doubt is that when i click on the plus sybmol i have called the select2 JS and it comes fine but i need to call the PHP function into it for looping of option value in the <select> . 我的疑问是,当我单击加号sybmol时,我已经调用了select2 JS,它可以正常使用,但是我需要在其中调用PHP函数以循环<select>option value

I need to generate dynamic options in this selected tag under my JS 我需要在JS下的此选定标签中生成动态选项

<select class="form-control select2" name="part_no[]" id="part_no" onchange="choose_parts(this.value);" required><option value="">Please Select</option></select>

My PHP function for getting the 我的PHP函数用于获取

function getAllActiveSpare($id)
{
    $query="";
    $conn=connectToDB();
    if(func_num_args()==1)
    {
     $query="SELECT * FROM `spare` WHERE `id`='".$id."' AND `status`='1' AND `delete_status`='0'";
    }
    else
    {
    $query="SELECT * FROM `spare` WHERE `delete_status`='0' AND `status`='1' ORDER BY `id` DESC ";
    }
    $results = $conn->query($query);
    $counts = $results->num_rows;
    if($counts==0)
    {
        return false;
    }
    else
    {
        $rows=[];
        while($row = $results->fetch_assoc())
        {
            $rows[] = $row;
        }
        return $rows;
    }   
}

I need to append this part alone in the JS Select TAG under append function. 我需要将此部分单独添加到附加功能下的JS Select TAG中。

<?php $parts_number = getAllActiveSpare();                      
if($parts_number==false)
{
?>
<option value="">No Parts Created Yet</option>
<?php  
}
else
{
?>
<option value="">Please Select</option>
<?php  
foreach ($parts_number as $key => $single_parts) {?>
<option value="<?php echo $single_parts['ref_part_no']; ?>"><?php echo $single_parts['ref_part_no']; ?></option>
<?php
}
}
?>                             
</select>

This above things alone i have tried. 这是我尝试过的事情。 Can anyone help me with this . 谁能帮我这个 。 I was struck up for the past two Days. 在过去的两天里,我感到非常震惊。

It seems you do not understand the difference between PHP and JavaScript, or the difference between the server and client side. 似乎您不了解PHP和JavaScript之间的区别,或者服务器端和客户端之间的区别。

Difference between Javascript and PHP Javascript和PHP之间的区别

To solve your problem, you probably need to use Ajax. 要解决您的问题,您可能需要使用Ajax。

https://stackoverflow.com/a/23740549/851885 https://stackoverflow.com/a/23740549/851885

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM