【问题标题】:In javascript how to send data to php file using post method在javascript中如何使用post方法将数据发送到php文件
【发布时间】:2023-03-03 17:34:01
【问题描述】:

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

【问题讨论】:

  • 对你的php文件使用ajax调用post方法!
  • 有很多关于这个主题的教程,你是先尝试在谷歌或其他搜索引擎中搜索你的问题吗?
  • 您忘记了表单中的操作参数。此外,要么将 onClick 函数附加到提交返回 true,要么将其完全删除。在您的情况下,最好删除 onclick 事件和 JS 代码,它应该自动允许 POST 到您的 PHP 页面。
  • 为什么你更喜欢JS调用你的php?只需将 action 属性添加到表单标签

标签: javascript php html


【解决方案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>

【讨论】:

    【解决方案2】:

    首先我需要说的是,您可以通过 ajax(无需重新加载页面)或直接发布(重新加载您的页面)发布您的数据。由于您的问题没有标记 jquery,所以我要离开 ajax。 这是发布表单数据的示例,我的意思是重新加载当前页面并将表单数据发布到profile.php 因此,您的 enctype 将是 enctype="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>

    【讨论】:

      【解决方案3】:

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

      打开 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);

      【讨论】:

        猜你喜欢
        • 2012-11-20
        • 1970-01-01
        • 2017-12-07
        • 2013-03-27
        • 2014-04-09
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多