2017-05-22 61 views
1

在javascript中我想知道如何使用post方法將數據發送到php文件。 我曾嘗試diffewrent方法。但我沒有得到任何輸出,所以請幫助我。 我的代碼如下所示。在javascript中如何使用post方法將數據發送到php文件

的index.php

<form method="post" name="form" enctype="multipart/form-data"> 
    <input type="text" name="name" id="name" placeholder="Name" required/> 
    <input type="email" name="email" id="email" placeholder="Email" required/> 
    <input type="password" name="pass" id="pass" placeholder="Password" required/> 
    <input type="submit" name="submit" value="Send" onclick="myFunction()"/> 
</form> 

<script> 
    function myFunction() { 
     var name = document.getElementById("name").value; 
     var email = document.getElementById("email").value; 
     var password = document.getElementById("password").value; 
     //// I want post the values to profile.php 
    } 
</script> 

profile.php

if (isset($_POST['submit'])) { 
    $name = $_POST['name']; 
} 
+1

使用Ajax調用方法後到PHP文件! – Ashwin

+3

有很多關於此主題的教程,您是否首先嚐試在谷歌或其他搜索引擎中搜索您的問題? – codtex

+1

http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php –

回答

1

做這樣的事情,並通過這樣你的價值觀,你可以在你的profile.php頁面檢查....在這裏,你可以不檢查if(isset($_POST['submit']))可以檢查if(isset($_POST['name']))

<form method="post" name="form" enctype="multipart/form-data"> 
    <input type="text" name="name" id="name" placeholder="Name" required/> 
    <input type="email" name="email" id="email" placeholder="Email" required/> 
    <input type="password" name="pass" id="pass" placeholder="Password" required/> 
    <input type="submit" name="submit" value="Send" onclick="myFunction()"/> 
    </form> 

    <script> 
    function myFunction() { 
    var name = document.getElementById("name").value; 
    var email = document.getElementById("email").value; 
    var password = document.getElementById("password").value; 
    $.ajax({ 
      type : "POST", //type of method 
      url : "profile.php", //your page 
      data : { name : name, email : email, password : password },// passing the values 
      success: function(res){ 
            //do what you want here... 
        } 
     }); 
    } 
    </script> 
0

做一些這樣的事情。在profile.php文件上發送阿賈克斯請求。

ON profile.php文件的print_r($ _ REQUEST)你會得到你的所有形式的索引。

$(document).ready(function() { 
 
    $("form").on("submit", function(event) { 
 
    $.ajax({ 
 
     type: 'POST', 
 
     url: 'profile.php', 
 
     data: $(this).serialize(), 
 
     success: function(data) { 
 
     //success code 
 
     } 
 
    }); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form method="post" name="form" enctype="multipart/form-data" onSubmit="return false;"> 
 
    <input type="text" name="name" id="name" placeholder="Name" required/> 
 
    <input type="email" name="email" id="email" placeholder="Email" required/> 
 
    <input type="password" name="pass" id="pass" placeholder="Password" required/> 
 
    <input type="submit" name="submit" value="Send" /> 
 
</form>

profile.php

的print_r($ _ REQUEST);

0

首先,我需要說,您可以通過ajax(無需重新加載頁面)或直接發佈(重新加載頁面)來發布數據。由於您的問題沒有標記jquery,所以我要離開ajax。 這裏是職位的表單數據的例子,我的意思是刷新當前頁面,併發布表單數據profile.php 因此您enctypeenctype="multipart/mixed"而不是enctype="multipart/form-data",因爲你不想發送任何文件輸入您的形式。

<form method="post" action="profile.php" name="form" id="your_form_id" enctype="multipart/mixed"> 
 
<input type="text" name="name" id="name" placeholder="Name" required/> 
 
<input type="email" name="email" id="email" placeholder="Email" required/> 
 
<input type="password" name="pass" id="pass" placeholder="Password" required/> 
 
<input type="button" name="btnsubmit" value="Send" onclick="myFunction()"/> 
 
</form> 
 

 
<script> 
 
function myFunction() { 
 
//you didn't need to get the data by `document.getElementById()`. just submit your form 
 
//var name = document.getElementById("name").value; 
 
//var email = document.getElementById("email").value; 
 
//var password = document.getElementById("password").value; 
 
    document.getElementById('your_form_id').submit(); 
 

 
} 
 

 
</script>

相關問題