0

I have a database that contains an autoincrement id, the location and the date's trip. With this code I can show on display the result of my query.

$mysqli = new mysqli("localhost", "root", "password", "trip");
$result = $mysqli->query("SELECT * FROM news WHERE location = '$location'");

echo "<br/><h3>"."Result, I found " .$result->num_rows. " results.". "</h3><br/>";


while($row = $result->fetch_assoc()) {
    echo "<tr><td>".$row["location"]."</td><td>".$row["date"]."</td><td>".' <button type="submit" value="reserve"/>'. "</td></tr>";
}

How can I know which button the user click to reserve his trip?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Zatarra97
  • 31
  • 4

3 Answers3

2
$('button').on('click',function(){
    alert($(this).val()); // do anything what you want

});

Sample snippet:-

$('button').on('click',function(){
    alert($(this).val()); // do anything what you want
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>pahse 7, mohali</td>
    <td>04/06/2019</td>
    <td><button value="Click Me!">Click Me</button></td>
  </tr>

Note:- instead of submit button use input type="button", as submit button used to submit form normally.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • 1
    How can I write, in the alert, a message like this: "You reserved a trip for $name_ of_the_trip the $date"? – Zatarra97 Jun 04 '19 at 10:41
  • @Zatarra97 you have to fist send that row data to php code so that some query will run and do proper changes.after that return success or failure of query execution. if success alert trip reversed otherwise alert trip cancellation error. and then try to debug and find out error and rectify them. – Alive to die - Anant Jun 04 '19 at 10:43
  • Can you help me with this? i never use Jquery, i have to put this code in a new page? – Zatarra97 Jun 04 '19 at 11:09
  • @Zatarra97 for that you have to ask a new question. your current question is already answered. Ask a new question with proper input data, your code effort, explanation of your problem and expected outcome also – Alive to die - Anant Jun 04 '19 at 11:11
1

You don't need jQuery, or JavaScript at all, unless you want to submit the form with AJAX, which you haven't mentioned. You just need to fix your HTML.

<button> is not an empty element; it needs a closing tag. The text on the button goes between the tags. Give the button a name and assign the row id as its value.

$button = "<button type='submit' name='id' value='$row[id]'>Reserve</button>";

Then you can get the id of the clicked button from $_POST['id'] in the PHP script that handles the form submit.


Also, with this code

$result = $mysqli->query("SELECT * FROM news WHERE location = '$location'");

there is no way SQL injection is not a problem, regardless of what you've done on the client side. Client side validation is trivial to bypass. You need to use a prepared statement.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

To get particular row information use data attribute in your button and then get that information

$mysqli = new mysqli("localhost", "root", "password", "trip");
$result = $mysqli->query("SELECT * FROM news WHERE location = '$location'");

echo "<br/><h3>"."Result, I found " .$result->num_rows. " results.". "</h3><br/>";


while($row = $result->fetch_assoc()) {
    echo "<tr><td>".$row["location"]."</td><td>".$row["date"]."</td><td><button type='button' data-bind='".$row["location"]." - ".$row["date"]."' value='reserve'/></td></tr>";

}

Use Jquery to get that information

$('button').on('click',function(){
  var info=$(this).attr('data-bind');
    alert(info); 

});
Balvinder Singh
  • 310
  • 2
  • 8