2016-04-22 18 views
1

Javaスクリプト関数 "display()"でphp関数 "insert_item()"を呼び出したい。submit.itのinsert_item()をクリックすると、関数私はSQLのクエリを使用してOracleデータのデータベースにデータを挿入したい。人々が私を助けることができますか?ありがとうございます。
ありがとうございます。私はjavascriptでphp関数を呼び出し、引数として2つの配列を渡します。

ここに私のコードです。

<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> 

+0

あなたはいくつかのメソッドを試しましたが、それらのメソッドは含まれていないと言います。おそらく最も有望な方法を含め、あなたが遭遇した問題を言うでしょう。 – Chris

+2

ページがブラウザにヒットすると、PHPは存在しなくなります。 PHP関数を呼び出す唯一の方法は、Ajaxを使用することです。これはあなたに検索用語を与えます。 –

+1

これはajaxで行う必要がありますhttp://stackoverflow.com/questions/24405486/execute-php-file-with-ajax –

答えて

2

Javascriptがクライアント側で実行中のPHPコードは、サーバー上で実行されます。 Webページがブラウザに到着すると、PHPは存在しなくなり、Javascriptはそれを認識しません。この機能を保持する別のWebページ(.php)を作成し、Ajaxを使用してJavascriptからこのWebページを呼び出す必要があります。 (これはバックエンドページと呼ばれます)。あなたのbackenページ(のみ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

+0

お返事ありがとうございます。 –

+0

答えを正しいものとして受け入れない理由はありますか?もっと援助が必要ですか? –

+0

絶対にうまくいきました。 –

関連する問題