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条)

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示