0

I have php form in while loop with different id.

<?php
while($pet = $results->fetch(PDO::FETCH_ASSOC)){
         ?>
<form method="post" name="petForm" id="petForm">
            <ul id="listAvatars">
                <li>
                <input type='text' name='pet' id='pet' value='<?=$pet['PetId']?>' />
                <img src="<?=PET_AVATAR.$pet['Avatar']?>" class="listAvatar">
                <span onclick='setPet(<?=$pet['PetId']?>)' id="avatarName"><?=$pet['PetName']?></span>
                </li>
            </ul>
</form>
           <?php
    }
?>

I want to get different pet id on click event. I am getting valid pet id on click name, by this function.

function setPet(petId){
    alert(petId);
    document.getElementById("petForm").submit();
}

PHP:

if(isset($_POST['pet'])){
    $petId = $_POST['pet'];
    print_r($petId);
}

By submitting this form, I am getting only the first pet's id. What should I do to get appropriate pet id on select?

2 Answers 2

2

Your loop is wrapping over separate form fields. When you click submit, it's only going to submit the respective form and not the others.

What you probably want to do is move the form fields outside of the loop, so that all petIds are passed. As it stands though, you're going to need to update the name to something like pet[] so PHP knows it's an array of petIds.

See: Posting array from form

Edit

It seems the real question is really:

When I click a petId, how do I POST that petId to a PHP script through a form (without the use of AJAX).

HTML

<div id="pet-id-1" class="petid">Pet 1</div>
<div id="pet-id-2" class="petid">Pet 2</div>
<div id="pet-id-3" class="petid">Pet 3</div>

<form action="" method="post" id="pet_form">
    <input type="hidden" name="pet_id" id="pet_id" value="" />
</form>

Javascript

$(document).ready(function() {
    $('.petid').click(function() {
        var pet_id = $(this).attr('id');
        $("#pet_id").val(pet_id);
        $("#pet_form").submit();
    });
});

See: http://jsfiddle.net/jwk2J/

Sign up to request clarification or add additional context in comments.

7 Comments

how to get appropriate values for each variables?
I tried it with making it array. I am getting all the pet ids. but, How can I identify in php which one is selected?
So you want to receive all of the petIds, but only care about one? Why not just send that one?
That's what I need to do. I have list of all the pets. I want to send only one which I will select. So, onclick i need to send that perticular pet id to php.
Ah, so when you click on a particular pet, you want it to fire (AJAX?) a request of to your PHP?
|
0

You need move <form method="post" name="petForm" id="petForm"> outside the while loop

 <form method="post" name="petForm" id="petForm">
      <input type='hidden' name='pet' id='pet' value='' />
      <ul id="listAvatars">
 <?php
    while($pet = $results->fetch(PDO::FETCH_ASSOC)){
     ?>

            <li>

            <img src="<?=PET_AVATAR.$pet['Avatar']?>" class="listAvatar">
            <span onclick='setPet(<?=$pet['PetId']?>)' id="avatarName"><?=$pet['PetName']?></span>
            </li>

       <?php
    }
 ?>
        </ul>
</form>

Javascript:

function setPet(petId){
   alert(petId);
   document.getElementById("pet").value=petId;
   document.getElementById("petForm").submit();
 }

1 Comment

In alert(petId), I am getting different pet id. but, in form submission, I am getting only the last element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.