Pass id to Modal Form from Bootstrap

Asked

Viewed 885 times

-1

I have a link that comes from a while in php and receives several lines of IDs. while puts id’s in the variable $dataid=$row['id'];

Receive the IDs on the link

$sql = "SELECT  u.id, u.username, u.genero, u.idade, u.local, u.descricao, u.status,u.last_login, u.photo_p_id, p.location 

FROM user AS u 

LEFT JOIN photos AS p

ON u.photo_p_id=p.id 
WHERE genero='m'
order by u.last_login DESC

LIMIT 8
";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
    $dataid=$row['id'];

    <a id='teste' data-id=".$dataid."  'class='teste' data-toggle='modal' data-target='#bannerformmodal'>teste</a>



    }else{
      echo "nada";
    }

When I click on the link and the modal form appears, the $dataid is not the same as the link referred to. It is always the last record that while returned.

Modal form

<div class='modal fade bannerformmodal' tabindex='-1' role='dialog' aria-  labelledby='bannerformmodal' aria-hidden='true' id='bannerformmodal'>
<div class='modal-dialog modal-sm'>
        <div class='modal-content'>
          <div class='modal-content'>
                <div class='modal-header'>
                <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                <h4 class='modal-title' id='myModalLabel'>Enviar mensagem</h4>
                </div>
                <div class='modal-body'>
                     <form id='requestacallform' method='POST' name='requestacallform' action="">
                                <div class='control-group'>
                                    <div class='controls'>                     

                                        <textarea id='msg' type='text' name='msg'  placeholder='Mensagem'><?php echo $dataid; ?></textarea>
                                </div>
                            </div>
                <button type='submit'  class='btn btn-blue'>Enviar</button>

                        </form>
                  </div>
              <div class='modal-footer'>
              </div>          
        </div>
        </div>
      </div>
    </div>

How to pass values IDs correct for the modal when I click on the link?

The links contain the IDS correct, only that when I go to the modal I lose the ID. How can I fix this?

1 answer

3


You have to put your HTML mount (which depends on repeat loop values while) within itself while... and when calling the modal, put a unique identifier for it, so that you don’t have multiple elements with the same ID

Something like that:

$sql = "SELECT  u.id, u.username, u.genero, u.idade, u.local, u.descricao, u.status,u.last_login, u.photo_p_id, p.location 
FROM user AS u 
LEFT JOIN photos AS p
ON u.photo_p_id=p.id 
WHERE genero='m'
order by u.last_login DESC
LIMIT 8
";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$dataid=$row['id'];
echo "<a id='teste' data-id='".$dataid."'  class='teste' data-toggle='modal' data-target='#bannerformmodal_".$dataid."'>teste</a>"; //Mudança aqui
}else{
  echo "nada";
}

Browser other questions tagged

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