【问题标题】:I want to call a php function in javascript and passing two arrays as argument in it.我想在 javascript 中调用一个 php 函数并在其中传递两个数组作为参数。
【发布时间】:2016-08-16 15:36:44
【问题描述】:

我想在 java 脚本函数“display()”中调用 php 函数“insert_item()”。我尝试了多种方法但无法做到。当我点击提交按钮时调用显示函数。IN insert_item( ) 功能我想使用 sql 查询在 oracle 数据数据库中插入数据。你们能帮帮我吗?
谢谢。

这是我的代码。

<html>
<head>
<BODY>
<p id="demo"> </p>
    <TITLE> Add/Remove dynamic rows in HTML table </TITLE>
    <SCRIPT language="javascript" type="text/javascript">      
            var a=["Naeem","Rehmat"];
            var b=[];`enter code here`
            var i=0;
            var c=1;
            function display() {
                for( var l=1;l<=i;l++)      //this loop fill the array a and b                  {  var x='task'+l;
                   var y=document.getElementById(x);
                   b[l]=y.value;
                   //alert(y.value);
                }
                for(var l=0;l<i;l++)
                { // document.write(a[l] +'  '+ b[l+1]+'<br>');
                }

    // here i want to call insert_item() function and want to pass a and b arrays as an argument; 
    //  here i want to call insert_item() function and want to pass a and b arrays as an argument;

     }

         function check(id)
         {          for (j=0;j<a.length;j++)
                    {
                       if(id==a[j])
                       { c=0;           }        }

        function addRow(tableID) {
            var x = document.getElementById("mySelect").selectedIndex;
            var y = document.getElementById("mySelect").options; 

            check(y[x].text);
            if(c){          
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "checkbox";
            element1.name="chkbox[]";
            cell1.appendChild(element1);

            var cell2 = row.insertCell(1);
            cell2.innerHTML = rowCount;

            var cell3 = row.insertCell(2); 
            cell3.innerHTML=y[x].text;      
            a[i]=y[x].text;
            i++;

            var cell4 = row.insertCell(3);                      
            cell4.innerHTML='<input type="text"  ' + ' name='+ '\"' +'task'+i+ '\"' + '  id=' + '\"' + 'task' +i + '\"'+' />';  }
            c=1;   
     }

        function deleteRow(tableID) {
           try {
           var table = document.getElementById(tableID);
           var rowCount = table.rows.length;

           for(var i=0; i<rowCount; i++) {
               var row = table.rows[i];
               var chkbox = row.cells[0].childNodes[0];
               if(null != chkbox && true == chkbox.checked) {
                   table.deleteRow(i);
                   rowCount--;
                   i--;               }
            }
            }catch(e) {
                alert(e);
            }
         }
     </SCRIPT>
<?php
   function insert_items($a,$b)
   {
      //echo "<br> Naeem <br>";
      //here i want to display a and b arrays data;  }
?> 
</BODY>

【问题讨论】:

  • 您说您尝试了多种方法,但似乎没有包含其中任何一种。也许包括最有希望的方法,并说出你遇到了什么问题。
  • 一旦页面进入您的浏览器,PHP 就不再存在了。调用 PHP 函数的唯一方法是使用 Ajax——它为您提供了一个搜索词。
  • Javascript 是客户端,PHP 是服务器端。如果您想向服务器发送内容并返回,则需要使用 ajax。

标签: javascript php jquery html ajax


【解决方案1】:

PHP 代码在服务器上运行,而 Javascript 在客户端运行。当网页到达浏览器时,PHP 不再存在,Javascript 无法识别它。您必须创建另一个网页 (.php) 来保存该功能并使用 Ajax 从 Javascript 调用此网页。 (这称为后端页面)。
在您的后台页面(仅限 php)中 - 假设它被称为 insert.php:

<?php
    function insert_items ($a, $b) {
        //Here you do your SQL database stuff
        //Make this value return what you want to print
    }

    echo insert_items($_GET["a"], $_GET["b"]);
?> 

现在在您的第一个页面中,您必须调用后端页面(使用 AJAX Http GET 请求 - 您可以阅读有关 ajax 请求的更多信息here

var a=["Naeem","Rehmat"];
            var b=[];`enter code here`
            var i=0;
            var c=1;
            function display() {
                for( var l=1;l<=i;l++)      //this loop fill the array a and b                  {  var x='task'+l;
                   var y=document.getElementById(x);
                   b[l]=y.value;
                   //alert(y.value);
                }
                for(var l=0;l<i;l++)
                { // document.write(a[l] +'  '+ b[l+1]+'<br>');
                }

    // here i want to call insert_item() function and want to pass a and b arrays as an argument; 
    //  here i want to call insert_item() function and want to pass a and b arrays as an argument;
    $.ajax({ //Assuming you work with jQuery
        url: "/insert.php?a=" + a.join(',') + "&b=" + b.join(','),
        type: "GET",
        success: function (data) {
            //Here you can do anything you want with the data retrieved. data is the string returned from the webpage (the value that was returned from the function insert_items())
        }
    });

     }

请注意,在 PHP 代码中,您不会将数组作为数组检索, 但作为带有 , 分隔符的字符串。你将不得不拆分那些 字符串返回到 PHP 中的数组中。

这种方式可以使用 ajax 请求进行前端-后端通信。如果你不使用jQuery,你可以看看如何发送ajax请求here

【讨论】:

  • 当然。是否有理由不接受正确的答案?您还需要更多帮助吗?
猜你喜欢
  • 1970-01-01
  • 2011-02-20
  • 1970-01-01
  • 2021-08-25
  • 1970-01-01
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多