dpxbc88022 2017-07-08 08:59
浏览 18
已采纳

将JS传递给PHP

I am trying to pass a string stored in a variable to a mysql table via php.

Currently I am using an <input> with type hidden, I assign the variable that I want as its value and post it through a form submit. it is working but it's ugly.

I know there is $.post and $.ajax but I don't seem to figure out how to use them in the js side and php side. I have looked for them online and there are a lot of questions of this sort but none of them work for me (probably because I am missing knowledge)

How can I do it?

  • 写回答

2条回答 默认 最新

  • duanjiong5023 2017-07-08 09:39
    关注

    Here is a very basic example. We start out with a form on an HTML page. When this button is clicked, we are going to activate a javascript function.

    <html>
      <form>
        <input type="email" id="email-field" />
        <input id="submitButton" type="submit" value="Submit" />
      </form>
    </html>
    

    Now, here is the javascript function being activated due to the button click. Inside, we extract any information that might have been filled out in the input field with id of "email-field", then send that off via ajax to a php file that sits on the server.

    $('#submitButton').click(function() {
      var email = $('#email-field').val();
    
      $.ajax({
        type: 'POST',
        url: './yourphpfilename.php',
        data: {
          email: email
        }
      }).done(function(data) {
        console.log(data) // Will send you the result that is echoed in the PHP file
      })
    })
    

    As long as you put the correct url in your ajax request to your PHP file, you can easily receive the data being sent like so,

    <?php
    
     if($_SERVER['REQUEST_METHOD'] == 'POST') {
    
       $email = $_POST['email'];
       echo 'I have received your request.';
    
     }
    

    To send the data back, I use the echo command to do so here.

    Try to read some documentation on the $_POST variable in PHP. Notice how I call for ['email']. The identifier inside the brackets directly correlates to the key inside the data object in the js file. For example, say we decided to name our email key something different in the js file.

    data: {
       useremail: email
    }
    

    You would then just change the PHP code like so,

    $email = $_POST['useremail'];
    

    This was very confusing for me starting out, and sometimes it's hard to even pose a quality question on it if you have no idea how it works. In the future though, I would atleast try to post some code showing that you attempted the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)