简体   繁体   English

如何使用Javascript在PHP中插入新字段并执行while循环

[英]How to use Javascript to insert new field in PHP and perform while loop

I have a form with the option to add another row at the click of a button. 我有一个表单,可以选择单击按钮添加另一行。 This new row will have a select list as it's input type. 此新行将具有选择列表作为其输入类型。 The select list needs to process information from a database that was retrieved on page load. 选择列表需要处理从页面加载时检索到的数据库中的信息。

How can I have the new select list perform a while loop on the data from the database once it is created via the add button. 通过添加按钮创建新选择列表后,如何对数据库中的数据执行while循环。

Here is the code I have so far. 这是我到目前为止的代码。

PHP: PHP:

echo "<div id=\"FieldGroup\">";

    echo "<select name=\"add_project_service_1\" class=\"project_details_service\" value=\"\" required >";
        while($result->fetch())
        {
            echo "<option value=\"".$item_number."\">".$item_number." - ".$description."</option>";
        }
    echo "</select>&nbsp;";

    echo "<label>Quantity: </label><input type=\"text\" name=\"add_project_quantity_1\" class=\"project_details_quantity\" placeholder=\"Quantity\" value=\"\" />&nbsp;";
    echo "<label>Value: </label><input type=\"text\" name=\"add_project_value\" class=\"project_details_value\" placeholder=\"Value\" value=\"\" /><br>";

echo "</div>";
echo "<input type=\"button\" value=\"Add Button\" id=\"addField\"><input type=\"button\" value=\"Remove Button\" id=\"removeField\">";

Javascript: Javascript:

<script>

$(document).ready(function() {

var counter = 2;

$("#addField").click(function () {
if(counter>50){
        alert("Only 50 extra fields allowed.");
        return false;
}   
var newFieldDiv = $(document.createElement('div'))
     .attr("id", 'FieldDiv' + counter);

newFieldDiv.after().html('<select name="add_project_service_' + counter + '" class="project_details_service" value="" required >' + 
'while($result->fetch())
     {
         echo "<option value=\"".$item_number."\">".$item_number." - ".$description."</option>";
     }</select>&nbsp;' + 
'<label>Quantity: </label><input type=\"text\" name=\"add_project_quantity_' + counter + '\" class=\"project_details_quantity\" placeholder=\"Quantity\" value=\"".$quantity."\" />&nbsp;' + 
'<label>Value: </label><input type=\"text\" name=\"add_project_value_' + counter + '\" class=\"project_details_value\" placeholder=\"Value\" value=\"".$value."\" /><br>');

newFieldDiv.appendTo("#FieldGroup");

counter++;

 });

 $("#removeField").click(function () {
if(counter==2){
      alert("No more fields to remove.");
      return false;
   }   

counter--;

    $("#FieldDiv" + counter).remove();
 });
});
</script>

Inserting the while loop into the javascript doesn't work. 将while循环插入javascript不起作用。

How can this be accomplished so when I add a field the options are listed and fields are populated? 如何完成此操作,以便在添加字段时列出选项并填充字段?

javascript is exectued on client side while php interpreted on the server side. javascript是在客户端执行的,而php是在服务器端执行的。

Once you've send the page to the client, the only way to edit the page without reloading a new one is with javascript and ajax call (xmlhttprequest). 将页面发送到客户端后,无需重新加载新页面即可编辑页面的唯一方法是使用javascript和ajax调用(xmlhttprequest)。 The client doesn't use php neither download php page from the server. 客户端不使用php,也不从服务器下载php页面。

You could do an ajax call to your page with jquery 您可以使用jQuery对页面进行ajax调用

$.ajax{
  url: "mypage.php",
  type: "GET",
  dataType: "jsonp",
  success: function( myObject ) {
    console.dir( myObject );
  }
}

// mypage.php
header('Content-type: application/json');
echo json_encode( $myObject ); 
// for you it will be
echo json_encode( $result->fetch() );

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

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