简体   繁体   English

将变量从js发布到表单,然后发布到php文件

[英]Post variables from js to form and then to php file

first of all, greetings and excuse my English... I explain my problem: I have some JS functions that generate some variables, I would like to send these variables via the POST method to a php file to be consulted in a database... I have read that the best way to do this is sending the variables to the respective values ​​about inputs within a form, Below I show the variables and the form: 首先,打招呼,请原谅我的英语...我解释我的问题:我有一些生成某些变量的JS函数,我想通过POST方法将这些变量发送到要在数据库中查询的php文件。我已经读到,做到这一点的最佳方法是将变量发送到表单中有关输入的各个值,下面我展示了变量和表单:

Javascript variables: On click in "Buscar" Javascript变量:在“ Buscar”中单击

            $('#Buscar').on('click', function () {
            document.getElementById("tipo").value=TipoDeInmuebleDATA.selectedData.value;
            document.getElementById("operacion").value=TipoDeOperacionDATA.selectedData.value;
            document.getElementById("habitaciones").value=HabitacionesDATA.selectedData.value;
            document.getElementById("MetrosCuadrados").value=MetrosCuadrados;
            document.getElementById("banos").value=BanosDATA.selectedData.value;
            document.getElementById("precio").value=Precio;
});

The Form: 表格:

        <form name="FormBuscar" method="post" action="consulta.php">
            <input id="tipo" name="tipo" type="hidden" />
            <input id="operacion" name="operacion" type="hidden" />
            <input id="MetrosCuadrados" name="MetrosCuadrados" type="hidden" />
            <input id="habitaciones" name="habitaciones" type="hidden" />
            <input id="banos" name="banos" type="hidden" />
            <input id="precio" name="precio" type="hidden" />
            <input type="submit" name="Buscar" class="BotonBuscar">
        </form>

I suspect that something is wrong, in the sense that I think the variables are not being sent to consultation In consultation, if I do the following: 我怀疑有问题,就我而言,如果我执行以下操作,则认为变量没有发送给咨询人员:

$tipo=$_POST['tipo'];
echo $tipo;

No result :-( 没有结果 :-(

Greetings, I will await your answers in! 问候,我将等待您的答复!

The event is wrong. 该事件是错误的。 You want to change the values right before submitting. 您要在提交前立即更改值。 So also add id="buscar-form" to the form tag. 因此,还要在表单标签中添加id="buscar-form" Then you can change the jQuery to this: 然后,您可以将jQuery更改为此:

$('#buscar-form').on('submit', function () {
  $('#tipo').val(TipoDeInmuebleDATA.selectedData.value);
  $('#operacion').val(TipoDeInmuebleDATA.selectedData.value);
  ...
});

Forget about the #buscar button. 忘记#buscar按钮。 When you click the button, the form is going to submit. 当您单击按钮时,表单将提交。 So that's the event captured above. 这就是上面捕获的事件。 Careful with your typing, there are a lot of typos! 小心输入,有很多错别字!

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

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