2016-09-22 19 views
0

我已經做了發佈/ JavaScript的XMLHttpRequest的成功更新()到PHP $ _REQUEST,如:後數據

function ajax_edit(e_id){ 

      var edit_form = document.getElementById('edit_form'+e_id); 

      var e_name = document.getElementById('name'+e_id).value, 
         e_email = document.getElementById('email'+e_id).value, 
         e_contact = document.getElementById('contact'+e_id).value, 
         e_status = document.getElementById('status'+e_id).value; 
        xmlhttp.open('GET', 'hello-world.php?edit=yes&id='+e_id+'&name='+e_name+'&email='+e_email+'&contact='+e_contact+'&status='+e_status, true); 
        xmlhttp.send(); 


      $('#edit'+e_id).modal('hide'); 
       return false; 
       edit_form.reset(); 
     } 

而且我的PHP的工作是這樣的:

if(isset($_REQUEST['edit'])){ 
    $name = mysqli_real_escape_string($conn, strip_tags($_REQUEST['name'])); 
    $email = mysqli_real_escape_string($conn, strip_tags($_REQUEST['email'])); 
    $contact = mysqli_real_escape_string($conn, strip_tags($_REQUEST['contact'])); 
    $status = mysqli_real_escape_string($conn, strip_tags($_REQUEST['status'])); 
    $edit_sql = "UPDATE users SET name = '$name', email = '$email', contact = '$contact', status = '$status' WHERE id = '$_REQUEST[id]'"; 
    $run_edit = mysqli_query($conn, $edit_sql); 
} 

現在我正試圖應用相同的過程到另一個Laravel 5.2項目,但不知道該怎麼做,特別是從我將數據發送到我的地方的部分url (hello-world.php?edit = yes)控制器作爲請求。

到目前爲止,我這個做:

  function submit_form(edit_id){ 

      xmlhttp = new XMLHttpRequest(); 

      var edit_form = document.getElementById('edit_form'+edit_id); 
      var url = "{{ URL::to('updatelabdetails'); }}"; 

      var edit_labname = document.getElementById('labname'+edit_id).value, 
       edit_pcname = document.getElementById('pcname'+edit_id).value; 

       alert(edit_pcname); 

      var params = "labname='+edit_labname+'&pcname='+edit_pcname"; 


       alert(params); 

       xmlhttp.open('GET', url+"?"+params, true); 
       xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

       xmlhttp.onreadystatechange = function() { 

       if(xmlhttp.readyState == 4 && http.status == 200) { 
          alert(xmlhttp.responseText); 

         } 
        } 
       xmlhttp.send(); 


       return false; 

     } 

但只能輸出至警報(edit_pcname);部分。

我的路線:

Route::post('updatelabdetails', '[email protected]'); 

我的控制器:

public function updateLabDetails(Request $request){ 
     $post = $request->all(); 
     var_dump($post); 
     die(); 
     } 

提交後它會像/showlabdetails某些網址是什麼?與MethodNotAllowedHttpException錯誤。

在此先感謝。

+0

您發送一個GET請求'xmlhttp.open( 'GET',網址+ +參數,真正的 「?」);'而路由被定義爲POST'路線::後(「updatelab ...'這就是爲什麼你得到* MethodNotAllowedHttpException *。 – TheFallen

+0

我檢查他們('GET,'POST')通過改變,但同樣的問題。請建議我的URL部分與「?」,因爲我沒有得到實際(params);因爲「var params」沒有得到正確的值。 –

回答

0

請更新您的submit_form功能這樣

function submit_form(edit_id){ 
    var edit_labname = document.getElementById('labname'+edit_id).value, 
      edit_pcname = document.getElementById('pcname'+edit_id).value; 

    var http = new XMLHttpRequest(); 
    var url = "updatelabdetails"; 
    var params = "labname='+edit_labname+'&pcname='+edit_pcname"; 
    http.open("POST", url, true); 

    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

    http.onreadystatechange = function() { 
    if(http.readyState == 4 && http.status == 200) { 
     alert(http.responseText); 
    } 
    } 
    http.send(params); 
} 
+0

它也沒有發生:( 請讓我知道這是如何[xmlhttp.open('GET','hello-world.php?編輯= yes&id ='+ e_id +'&name ='+ e_name +'&email ='+ e_email +'&contact ='+ e_contact +'&status ='+ e_status,true);;]代碼類型必須寫入laravel 5.2控制器的刀片模板中要求。 –

+0

'xmlhttp.open('GET','hello-world.php?edit = yes&id ='+ e_id +'&name ='+ e_name +'&email ='+ e_email +'&contact ='+ e_contact +'&status = '+ e_status,true);' 這是一個* GET *請求,這就是爲什麼我們可以用鍵值對發送* Query String *。 –

+0

好吧我把這兩個得到/後同js&路線,現在請告訴我如何將這個xmlhttp.open()代碼laravel控制器。什麼將替換[hello-world.php?edit = yes] –

0

你laravel ROURE是不正確的,你已經註冊POST路由和訪問GET之一。

變化Route::post('updatelabdetails', '[email protected]');Route::get('updatelabdetails', '[email protected]');

+0

laravel 5.2控制器將會替換[hello-world.php?edit = yes]? –

+0

在JavaScript中 '''js xmlhttp.open('GET','hello-world?edit = yes&id ='+ e_id +'&name ='+ e_name +'&email ='+ e_email +'&contact ='+ e_contact +'&status ='+ e_status,true); ''' 在路線 '''php Route :: get('hello-world','YourController @ methodName'); ''' –