简体   繁体   English

多个 forms 无需重新加载页面

[英]Multiple forms without reloading the page

I need to do this.我需要这样做。

I am using php.我正在使用 php。 i need to send a post to the server every time the user sends the result of a match, without reloading the page每次用户发送匹配结果时,我都需要向服务器发送帖子,而无需重新加载页面

I've been trying jquery but I couldn't.我一直在尝试 jquery 但我做不到。

This is my form:这是我的表格:

<?php

$sqlpartidos =mysqli_query($con, "SELECT * FROM partidoprode WHERE idfecha = '$idfecha';");

while($filapartidos = mysqli_fetch_array($sqlpartidos)){

  echo '<div class="list-group-item list-group-item-action flex-column ">
  <div>

    <div class="row justify-content-center">

      <div class="col-3" id="bloqueequipo">
      <div><h5 class="nombreequipos">'.$filapartidos['nombrelocal']. ' '.' </div><div><img src="'.$filapartidos['imglocal'].'" alt="" class="imgequipos"></h5></div> 
      </div>
      <div class="col-6" id="bloqueequipo"><form id="myForm" method="post"><input type="number" id="reslocal" name="reslocal" min="0" max="10" size="1"/> - <input type="number" id="resvisitante" name="resvisitante" min="0" max="10" size="1"/><input type="hidden" name="idpartido" id="idpartido" value="'.$filapartidos['id'].'"/><input type="hidden" name="idfecha" id="idfecha" value="'.$filapartidos['idfecha'].'"/><div id="botondiv"><input type="submit" value="Submit" /></div></form> <div id="results"></div></div>
      <div class="col-3" id="bloqueequipo">
      <div><h5 class="nombreequipos">'.$filapartidos['nombrevisitante']. ' '.' </div><div><img src="'.$filapartidos['imgvisitante'].'" alt="" class="imgequipos"></h5></div> 
      </div>

    </div> 

  </div>
  <div class="col-12 text-muted nombreequipos"><small>'.$filapartidos['fecha'].'</small></div>

  </div>';
}
  ?>

EDIT: I was trying to do this.编辑:我试图这样做。

function SubmitFormData() {
    var idpartido = $("#idpartido").val();
    var idfecha = $("#idfecha").val();
    var reslocal = $("#reslocal").val();
    var resvisitante = $("#resvisitante").val();
    $.post("enviar_jugada.php", { idpartido: idpartido, idfecha: idfecha, reslocal: reslocal, resvisitante: resvisitante },
    function(data) {
     $('#results').html(data);
     $('#myForm')[0].reset();
    });
}

And the input和输入

<input type="button" id="submitFormData" onclick="SubmitFormData();" value="Submit" />

The above code is very messy and riddled with errors ( indent your code correctly will help you immensely? ) but perhaps the following approach might help?上面的代码非常混乱并且充满了错误(正确缩进你的代码会极大地帮助你吗?)但也许下面的方法可能会有所帮助?

If you use printf or sprintf etc you can create quite clean looking markup that has placeholders into which variables are plugged - which makes it much easier to read imo.如果您使用printfsprintf等,您可以创建非常干净的标记,其中包含插入变量的占位符 - 这使得 imo 更容易阅读。 Remove All the ID attributes that you generate in the loop - you do not generally need them as you can access HTML elements using querySelector and sibling selectors.删除您在循环中生成的所有 ID 属性 - 通常不需要它们,因为您可以使用querySelector和同级选择器访问 HTML 元素。 In this way you can capture a (node)list of submit buttons and assign the event handler remotely rather than inline通过这种方式,您可以捕获提交按钮的(节点)列表并远程分配事件处理程序而不是inline

The original markup is, as I say, messy so the following might not be accurate to your original intentions.正如我所说,原始标记很混乱,因此以下内容可能不符合您的初衷。

while( $filapartidos = mysqli_fetch_array( $sqlpartidos ) ){

    printf('
    <div class="list-group-item list-group-item-action flex-column">
        <div>
            <div class="row justify-content-center">

                <div class="col-3 bloqueequipo">
                    <div>
                        <h5 class="nombreequipos">%s</h5>
                        <img src="%s" alt="" class="imgequipos" />
                    </div>
                </div>

                <div class="col-6 bloqueequipo">
                    <form method="post">
                        <input type="number" name="reslocal" min="0" max="10" size="1" /> - 
                        <input type="number" name="resvisitante" min="0" max="10" size="1" />
                        <input type="hidden" name="idpartido" value="%s" />
                        <input type="hidden" name="idfecha" value="%s" />
                        <div class="botondiv">
                            <input type="submit" />
                        </div>
                    </form>
                    <div data-id="results"></div>
                </div>

                <div class="col-3 bloqueequipo">
                    <div>
                        <h5 class="nombreequipos">%s</h5>
                        <img src="%s" alt="" class="imgequipos" />
                    </div>
                </div>

                <div class="col-12 text-muted nombreequipos">
                    <small>%s</small>
                </div>

            </div>
        </div>
    </div>',

        /* assign the variables / data to the placeholders */
        $filapartidos['nombrelocal'],
        $filapartidos['imglocal'],
        $filapartidos['id'],
        $filapartidos['idfecha'],
        $filapartidos['nombrevisitante'],
        $filapartidos['imgvisitante'],
        $filapartidos['fecha']

    );//end printf

}//end while loop

<script>
    const ajax=function(url,data,callback){
        let xhr=new XMLHttpRequest();
            xhr.onreadystatechange=function(e){
                if( this.status==200 && this.readySate==4 )callback( this.response );
            }
            xhr.open( 'POST', url, true );
            xhr.setRequestHeader('X-Requested-With','XMLHttpRequest');
            xhr.send( data )
    }

    Array.prototype.slice.call( document.querySelectorAll( 'form > div > input[type="submit"]' ) ).forEach( bttn=>{
        bttn.addEventListener('click',function(e){
            e.preventDefault();
            let fd=new FormData( e.target.parentNode.parentNode );
            let results=e.target.parentNode.parentNode.parentNode.querySelector('[data-id]');
            ajax( location.href, fd, function(r){
                alert(r);
                results.innerText=r
            });
        });
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM