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

需要从表单中获取输入,使用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!

这是我的问题。 p>

我有一个关于这个地址的首页网站 : http://battlestation.rocks/ p>

它主要围绕着 执行命令的首页中的搜索栏。 目前,每个命令都用php处理,如下所示: p>

  if(isset($ _ POST ['searchBar'])){
 $ originalcommand = $ _POST ['searchBar  ']; 
 processcommand($ originalcommand); 
 return; 
} 
  code>  pre> 
 
 

这样,每个命令都会重新加载页面,这是最浪费的 它只是从头开始打开一些链接。 在其他情况下,命令会更改起始页,因此在这些情况下我希望页面重新加载。 p>

我从其他问题中看到,您可以将页面重置 使用AJAX重新加载,但我看到的答案都没有将表单输入发送到php进行处理,也没有包含必要时重新加载的选项。 p>

我非常喜欢 业余爱好者编码器并没有使用AJAX的经验,请不要对我有太多的技术。 我对这个起始页很着迷,我只需要让它按预期工作。 请帮忙! p> div>

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;

?>

?>

Yes, you should send the data via AJAX. One popular option is to use jQuery. Below is a simplified example using jQuery.

First, the HTML:

 <input id="searchBar" name="searchBar" type="text" /> 
 <button id="submit">Search</button>
 <div id="searchResults"></div>

Next the jQuery:

<script type="text/javascript">
    $("#submit").click(function() {
        $.ajax({
            type: "post",
            url: "search.php", // Your PHP file
            data: {
                searchBar:  $("#searchBar").val()
            },
            success:function(results) {
                 // Do something with your results
                 $("#searchResults").html(results);
            }
        });
    });
</script>