dongna9185 2017-02-28 22:47
浏览 34
已采纳

需要从表单中获取输入,使用php处理它,具有根据答案重新加载页面的选项

this is my problem.

I have a startpage website on this address: http://battlestation.rocks/

It mostly revolves around a search bar in the front page that executes commands. Presently, every command is processed with php, like so:

if (isset($_POST['searchBar'])) {
    $originalcommand = $_POST['searchBar'];
    processcommand($originalcommand);
    return;
}

With this, every command reloads the page, which is a waste as most of the times it just opens some link from the startpage. In other cases, the startpage is changed by the commands, and thus in those cases I would like the page to reload.

I've seen from other questions that you can have the page not reload with AJAX, but none of the answers I've seen send the form input to php for processing, nor do they include the option to reload if necessary.

I'm very much a hobbyist coder and have zero experience with AJAX, please don't get too technical on me. I'm obsessed with this startpage, I just need to get this working as intended. Please help!

  • 写回答

2条回答 默认 最新

  • dougan7657 2017-02-28 23:20
    关注

    If you use PHP directly then the page needs to reload. If you use AJAX then you can send it so without reloading. !-You need a new PHP file in which you process the input-! An example with jquery but works the same:

    You normal site:

    <form action=''>
            <input type='email' id='email' placeholder='E-Mail' required=""/>
            <input type='password' id='pwd' placeholder='Passwort' required=""/>
    
            <button id='go_login'>Go</button>
    </form>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
      $('#go_login').click(function() {
    
        var bn = $('#email').val();
        var pw = $('#pwd').val();
    
        $.post('newphpsite.php', {'bn': bn, 'pw': pw}, function(data) {
           alert(data); //send back echo from PHP
        });
    
       return false; //The form is not sent and does not reload the page.
     });
    </script>
    

    newphpsite.php :

    <?php 
    
      $bn = $_POST['bn'];
      $pw = $_POST['pw'];
    
      echo $bn."".$pw;
    
    ?>
    

    ?>

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

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择