在javascript中如何使用post方法将数据发送到php文件 [英] In javascript how to send data to php file using post method

查看:31
本文介绍了在javascript中如何使用post方法将数据发送到php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中,我想知道如何使用post方法将数据发送到php文件.我尝试了不同的方法.但我没有得到任何输出,所以请帮助我.我的代码如下所示.

In javascript i want to know how to send data to php file using post method. I have tried diffewrent method. But i didn't get any output so please help me. My code is shown below.

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

推荐答案

做这样的事情并像这样传递你的值,你可以检查你的 profile.php 页面......在这里你不能检查 if(isset($_POST['submit'])),但是你可以检查if(isset($_POST['name']))

Do something like this and pass your values like this and you can check in your profile.php page....Here you cannot check if(isset($_POST['submit'])), but you you can check 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>

这篇关于在javascript中如何使用post方法将数据发送到php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆