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 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算