0

i got this annoying problem i can't figure out how to solve

After a quick research i found those links bellow, which i could partially make work

verifying if checkbox is checked in php

How to read if a checkbox is checked in PHP?

This is my html:

<form method="post">

<!-- CHECK BOX TIPO DE PRODUTO (INTEIRO/PEDAÇO) !-->
<input type="checkbox" name="stipo" id="stipo" value="1" onchange="document.getElementById('tipo').disabled = !this.checked;">Tipo         
<select name="tipo" id="tipo" disabled="false">
 <option value="Inteiro">Inteiro</option>
 <option value="Pedaço">Pedaço</option>
</select><br>

<input type="submit" href="/consulta/consulta_produto.php" class="loader" value="Consultar">

</form>

and this is my php:

if ($_POST['stipo'] == 1){
echo "123";
} else {
echo "456";
}

Because at the Html code im using form like this:

<form method="post">
</form>

and making the post with a submit button like that:

<input type="submit" href="/consulta/consulta_produto.php" class="loader" value="Consultar">

It doesn't work due to the Href at the submit button.

However, if i do the Html code like this:

<form action="/consulta/consulta_produto.php" method="post">

<input type="submit" class="loader" value="Consultar">

</form>

It will work. But, the problem of the form above is that my script stop working. I Cant use the method above because i need Href into the submit button, because when i submit the form, im loading the next page inside a Div

Just in case it helps, this is my Javascript and why i need the Href

<script type="text/javascript">
$(function() {
    $('.loader').click(function(event) {
        event.preventDefault(); // stop the link loading the URL in href
        $('#content').load($(this).attr('href'));
    }); 
});
</script>

Anyone got some idea on how to make the POST method works with href instead of action on the form?

Kouhei
  • 137
  • 4
  • If you load the next page into a div, how is the form submitted? Sounds like what you need is ajax. – GrumpyCrouton Oct 29 '18 at 18:31
  • You can make the POST method work with an HREF using AJAX, instead of the form action. – Jay Blanchard Oct 29 '18 at 18:36
  • Use `$(".loader").parent("form").submit` instead of `$(".loader").click` – Vivick Oct 29 '18 at 18:36
  • well, i tought it would submit like that, but seems like i was wrong... maybe i have to try with ajax itself – Kouhei Oct 29 '18 at 18:37
  • Vivick, it seems like to be posting now, but the page doesnt load after that, i dont know if i messed up the code, could you please check if is that right? i named the form with "teste" and then changed the script to the following ------> $(function() { $('.loader').parent("teste").submit { event.preventDefault(); // stop the link loading the URL in href $('#content').load($(this).attr('href')); }); }); – Kouhei Oct 29 '18 at 18:44

0 Answers0