PeterP PeterP - 1 year ago 67
Ajax Question

Ajax, Jquery and PHP... I am stuck

I am trying some things regarding Ajax, Jquery and PHP and after fiddeling for two days I am stuck, can someone please help me.

My javascript code (the pop.php is located correctly)

<script type="text/javascript">
$('.nameclick').click(function()) {
ev.preventDefault();
$.ajax({
typ: "POST"
url: "/pop.php"
data: {id=$(this).data("formid")}
success: function(result){
alert(result);
} else {
alert("test");
}
});
});
</script>


Part of my html/php code in the same page (this code works fine to print out the table I am looking for):

<table class="data-table">
<thead>
<tr>
<th>Servicenummer</th>
<th>Kund</th>
<th>Uppgift</th>
<th>Status</th>
<th>Inlämnad</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
echo '<tr>
<td><div id="clickMe">'.$row['service'].'</div></td>
<td><a href="#" class="nameclick" data-formid="'.$row['ID'].'">'.$row['name'].'</a></td>
<td>'.$row['Drakt1'].'<br>'.$row['Drakt2'].'<br>'.$row['Drakt3'].'<br>'.$row['Drakt4'].'<br>'.$row['Drakt5'].'<br>'.$row['Drakt6'].'<br>'.$row['reg1'].'<br>'.$row['flaska1'].'<br>'.$row['dator1'].'</td>
<td>'.$row['Service_Status'].'</td>
<td>'.$row['date'].'</td>
</tr>';
$no++;
}?>
</tbody>
</table>


And my pop.php:

<?php
require 'connection.php';
$conn = Connect();
$name = $conn->real_escape_string($_POST['name']);
$email = $conn->real_escape_string($_POST['email']);
$telephone = $conn->real_escape_string($_POST['telephone']);

$sql = 'SELECT name, email, telephone FROM Service_Form WHERE id = "10"';

$query = mysqli_query($conn, $sql);

if (!$query) {
die ('SQL Error: ' . mysqli_error($conn));
}
?>

<?php
while ($row = mysqli_fetch_array($query))
{
echo $row['name'];
echo $row['email'];
echo $row['telephone'];
}
?>


At the moment when I click my link in my normal page i dont get anything, no alerts at all, but my pop.php prints out the values correctly, i hope... I know I have to change my id="10" to something but i am not certain to what, could it be formid?

What am I doing wrong here?

What I want:
* a table with the values below from a table in the database
* when i click the name in the table it should show an alert (or something similar) that shows the name, email and telephone number (these are stored as well in the database table but not on the website table).

Thank you in advance!

Answer Source

your ajax function is having some errors

so use this

 <script type="text/javascript">
    $('.nameclick').click(function(ev) {
        ev.preventDefault();
        var id = $(this).data("formid");
        $.ajax({
            type: "POST",
            url: "pop.php",
            data: {
                  "id":id
                 },
            success: function(result){
                alert(result);
            },
            error:function (error) {
                alert(error);
            }
        });
    });
</script>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download