0

I already have a table that shows only one data, after clicking a button(edit) i need it to show/display the other data from the database through modal. My problem is(for now), i cant fetch the ID for each row in the html table. (once i can get the ID i think i can fetch the other datas)

Heres the PHP Code that show the table:

<?php
$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
mysql_select_db('ts_php');
$query = "SELECT * FROM job_posted";
$result = mysql_query($query);

echo "<table class='table'>
          <thead>
              <th>JOB</th>
              <th>STATUS</th>
              <th>APPLICATIONS</th>
              <th>EDIT</th>
              <th>DELETE</th>
          </thead>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
    echo "<tr>
              <th>" . $row['job_title'] . "</th>
              <td>" . $row['status'] . "</td>
              <td>" . $row['applications'] . "</td>
              <td><a data-toggle='modal' data-target='#myModal'>edit</a></td>
              <td><a href='#'>delete</a></td>                  
          </tr>";
}
echo "</table>";
mysql_close();
?>
executable
  • 3,365
  • 6
  • 24
  • 52
jagaimono
  • 9
  • 2
  • 1
    Regarding the usage of the `mysql_*` functions in PHP, have a look at the answer to the question [Why shouldn't I use mysql_* functions in PHP?](https://stackoverflow.com/a/12860046/7008354) – Tobias F. Nov 06 '18 at 09:00

4 Answers4

0

I think you are talking about the primary key id of the job_posted table. Add id data attribute in your a tag and then access that id on the click of it.

<td><a data-toggle='modal' data-target='#myModal' data-id='" . $row['id'] . "'>edit</a></td>

Replace $row['id'] with you id key.

Kamal Paliwal
  • 1,251
  • 8
  • 16
0

For an example your primary_key is id. you need to change the href which is open modal to this

    <tr>
        <th>" . $row['job_title'] . "</th>
        <td>" . $row['status'] . "</td>
        <td>" . $row['applications'] . "</td>
        <td><a data-toggle='modal' data-id='".$row['id']."' data-target='#myModal'>edit</a></td>
        <td><a href='#'>delete</a></td>                  
    </tr>";

and i think you need to change the way you open modal too. First adding class to your href for an example i will use .openeditmodal.

so your href will look like this

<a class='openeditmodal' data-toggle='modal' data-id='".$row['id']."' data-target='#myModal'>edit</a>

then add this line to open modal.

  $(".openeditmodal").click(function(){
    var job_posted_id = $(this).data('id');
    $("#myModal").modal("show");
});
Boby
  • 1,131
  • 3
  • 20
  • 52
  • May i ask why i should i change the way i open my modal? what the difference? Thanks in advance – jagaimono Nov 06 '18 at 09:41
  • @sasugaAndrei Well, with that you can send `data-id` from your `href` . – Boby Nov 06 '18 at 10:04
  • yes and then inside the modal was the other data from that row. but i cant display the data maybe because the $row['id'] is inside the while loop. i dont have any problem with displaying my modal but thanks for that ill use that for future reference cheers – jagaimono Nov 06 '18 at 10:06
  • try to use `ajax` . so when the user click `edit` the `ajax` will populate the data, then you can show the data to your modal – Boby Nov 06 '18 at 10:07
  • Okay, i just reviewed the link you've given to me, ill try to do it at home(sadly i need to go home now) Thank you very much for this, ill reply again if it works smoothly. – jagaimono Nov 06 '18 at 10:15
  • I tried the making this one, but the result of job_posted_id was undefined. sorry for the delayed response EDIT: i change your code var job_posted_id = $(this).data('id'); into var job_posted_id = $(this).data('post_id');(which is my dataname. then i change it back into 'id' and it works! – jagaimono Nov 07 '18 at 14:08
  • glad to hear that – Boby Nov 08 '18 at 02:06
0

Try this code.

In inside your loop

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
echo "<tr>
          <th>" . $row['job_title'] . "</th>
          <td>" . $row['status'] . "</td>
          <td>" . $row['applications'] . "</td>
          <td><a data-toggle='modal' data-target='#myModal' data-id='" . $row['id'] . "'>edit</a></td>
          <td><a href='#'>delete</a></td>                  
      </tr>";
}

In your script

$('#myModal').on('show.bs.modal', function(e){
    var id= $(e.relatedTarget).data('id');
    console.log(id);

});
jaZzZ
  • 127
  • 12
-1

you can get the id in hidden fields.

'<input type="hidden" value =' . $row["id"] . ' >'

you need to create primary key, auto increment in your database first column i.e your $row["id"]

<td><a data-toggle='modal' data-target='#myModal' data-id='" . $row['id'] . "'>edit</a></td>

for your code check below.

while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
    echo "<tr>
        <th>" . $row['job_title'] . "</th><td>" . $row['status'] . "</td><td>" . $row['applications'] . "</td>
                    <td><a data-toggle='modal' data-target='#myModal' data-id='" . $row['id'] . "'>edit</a></td>
                    <td><a href='#'>delete</a></td>                  
                    </tr>";
    }
Sayed Mohd Ali
  • 2,156
  • 3
  • 12
  • 28