在javascript中如何使用post方法将数据发送到php文件

时间:2017-05-22 05:50:29

标签: javascript php html

在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']; 
}

3 个答案:

答案 0 :(得分:1)

执行此类操作并传递您的值,然后您可以查看您的个人资料.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>

答案 1 :(得分:0)

做这样的事情。在 profile.php 文件上发送ajax请求。

ON profile.php 文件 print_r($ _ REQUEST)您将获得所有表单索引。

&#13;
&#13;
$(document).ready(function() {
  $("form").on("submit", function(event) {
    $.ajax({
      type: 'POST',
      url: 'profile.php',
      data: $( this ).serialize(),
      success: function(data) {
        //success code
      }
    });
  });
});
&#13;
<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>
&#13;
&#13;
&#13;

<强> profile.php

的print_r($ _ REQUEST);

答案 2 :(得分:0)

首先我需要说的是,您可以通过ajax(无需重新加载页面)或直接发布(重新加载页面)来发布您的数据。由于您的问题没有标记jquery,所以我要离开阿贾克斯。 以下是发布表单数据的示例,我的意思是重新加载当前页面并在profile.php中发布表单数据 因此,您的enctypeenctype="multipart/mixed"而不是enctype="multipart/form-data",因为您不想在表单中发送任何文件输入。

&#13;
&#13;
<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>
&#13;
&#13;
&#13;