submit php variable and form with single Submit

Asked

Viewed 143 times

0

good evening everyone,

I’m doing a job at my college and I’m having a hard time at one part. I’ve researched and tried several ways, as I’m starting maybe there’s another way that I don’t know.

I have two pages index.php and localize.php, I need to send the variable id_pet to the page locate, this I did using get by the link URL so far I managed to do quiet ta working. But I also need to send the initial and final date on a form. I wanted to do this using the same button as I use to send id_pet. As my upload button is outside the form I used Jquery to submit the form and it worked using this code since the input type Submit is outside the tag "a" so it submits the form. Below is the jquery code:

$(document).ready(function(){
   $(".btnLocalizar").click(function(){
    $("#formData").submit();
           });
       });

but as I want to send everything with a button I tried to do the following:

while($row = mysqli_fetch_array($resultado)){
     echo "<tr>";
     echo "<td>" . $row["id_pet"]."</td>";
     echo "<td>" . $row["nome_pet"]."</td>";
     echo "<td>" . $row["nome_tutor"]."</td>";
     echo "<td>" . "<a href='localizar.php?id_pet=$row[id_pet]'>";
     echo "<input class='btn btn-primary btnLocalizar' type='submit' value='Localizar'>";
     echo "</a>";
     echo "</td>";
            }

This php code generates my table with the result of my mysql test database and already includes in the table the link/Submit that sends the id_pet variable to the page I need. But when this way I click on the button and it sends only the php variable and does not submit my page form using the javascript code from above.

Only I didn’t understand, if I create an input of type Submit outside the tag "a" the code Jquery works normal and submits the form, but then I have no way to send the php variable.

I hope it was clear my doubt, I need to send the form data and the php variable using a single Submit or link if and what to do this.

from now on thanks

  • Where the input is outside the form

  • Because it is inside a Voce form to add the variable in an Hidden input: eg: <input type='hidden' value='<?php echo $id_pet ?>' name='id_ped'>

  • Or Voce wants to send all table data?

  • when you keep the input inside the "a" tag and try to give a Ubmit, some error appears in the console?

  • this one and the input line: echo "<input class='btn btn-primary btnLocalizar' type='submit' value='Localizar'>"; i need to send my mysql table only id_Pet, but I wanted to send together the contents of my form that is before this line of Submit

1 answer

1


Before the answer itself, I would like to make a few comments:

  • If the button is out of the form, there’s no point in it being like submit. Change the type to button, why it is only the trigger to submit the form manually.

  • Don’t put a button inside a tag <a></a>. Button is one thing, link is another. Each has a different function.

Let’s solve:

Make sure that the form owns the method="post", because you will receive the form data in PHP via $_POST. Only the id_pet will be via $_GET.

Include an attribute in the button data-id with the id in question:

echo "<input data-id='". $row["id_pet"] ."' class='btn btn-primary btnLocalizar' type='button' value='Localizar'>";
                ↑

And in jQuery you will change the action of form with the value of this data-id button click before submitting the form:

$(".btnLocalizar").click(function(){
   $("#formData")
   .attr("action", "localizar.php?id_pet="+$(this).data("id"))
   .submit();
});

Do not forget to remove from the code the <a href='localizar.php?id_pet=$row[id_pet]'> and the closure </a>.

  • Thanks solved the problem, worked perfectly. If you could just explain this little bit of code to me, I think I got it partially, but I wanted to make sure I got it right. "$(this).data("id"))

  • Cool q solved. The code is as follows: $(this) is the button clicked, the date("id") takes the value of the data-id attribute... If it were a date-something, it would be $(this). date("something")

  • thanks for the explanation, helped too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.