0

I want to have an ID being linked with the clicked result from my output but I want java script to get the link to avoid calling another page. My old way of coding is like this, I have cut the other info.

$id=$row["IndexNo"];
echo '
    <tr>
    <td>'. $row["IndexNo"] . '</td>
    <td>'. $row["SurName"] . '</td>
    <td>'. $row["FirstName"] .'</td>
    <td><a href="update.php?IndexNo='. $id . '">view</a></td>
    </tr>';

this way the page update.php is loaded with a URL:.../update.php?IndexNo=1. I want to avoid this page load by introducing java script like

$(document).ready(function(){
    $("#update").click(function(){
        $("#content").load("update.php");
    });
});

the thing is I don't seem to know how to link the two on click of what I should put in the href but at the same time call the ID to only call a distinct result for that selected IndexNo.

1
  • 2
    Your question is not clear?please try to write what you want point wise? Commented Apr 1, 2015 at 8:32

4 Answers 4

1

I think you want like this :-

<td><a class= 'update' href="update.php?IndexNo='. $id . '">view</a></td>

so when click on a update link reqeust goes to update.php?IndexNo={$id} and content will load in #content element of page, and page will not refresh

$(document).ready(function(){
     $(document).on('click', '.update',function(){
            $("#content").load($(this).attr('href'));
            return false;  // __prevent from default action            

     });
});

Thanks

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

3 Comments

that still didn't work, it was overpowered and the page update.php?IndexNo=1 loaded. I also had to put double quotes on the class="update" to escape a syntax error. Lets try more
Tiger, It worked my guy I was miss matching the class name. I standardized the update in php and js. Cheers
I now have another problem, how do I get the IndexNo using the $_get method like: $IndexNo = $_GET['IndexNo']; as the URL is hidden? I want to use this to complete my query like: $sql = "SELECT * FROM student_information WHERE IndexNo='$IndexNo'";
0

As I understood, you don't want to redirect the page while clicking on the anchor tag. Is that ? Then, please replace your code with any one of below set:

$(document).ready(function(){
    $("#update").click(function(){
        $("#content").load("update.php");
        return false;
    });
});

or

$(document).ready(function(){
    $("#update").click(function(e){
        e.preventDefault();
        $("#content").load("update.php");
    });
});

Thanks

1 Comment

I now have another problem, how do I get the IndexNo using the $_get method like: $IndexNo = $_GET['IndexNo']; as the URL is hidden? I want to use this to complete my query like: $sql = "SELECT * FROM student_information WHERE IndexNo='$IndexNo'";
0

if you want to update the current data without refreshing the current page you need to use ajax.With ajax you can send the data(id) to another page with out refreshing.

2 Comments

//ignore previous comment $id=$row["IndexNo"]; echo ' <tr> <td>'. $row["IndexNo"] . '</td> <td>'. $row["SurName"] . '</td> <td>'. $row["FirstName"] .'</td> <td><button id="redirect" onclick="hai('<?php print $id; ?>')">view</button></td> </tr>'; function hai(id){ $.ajax({ url: "update.php", type: 'POST', data: { "id":id }, success: function (data) { alert(data); // display result } }) }
I now have another problem, how do I get the IndexNo using the $_get method like: $IndexNo = $_GET['IndexNo']; as the URL is hidden? I want to use this to complete my query like: $sql = "SELECT * FROM student_information WHERE IndexNo='$IndexNo'";
0
$id=$row["IndexNo"];
echo '
    <tr>
    <td>'. $row["IndexNo"] . '</td>
    <td>'. $row["SurName"] . '</td>
    <td>'. $row["FirstName"] .'</td>
    <td><button onclick="hai('<?php print $id; ?>')">view</button></td>
    </tr>';

//ajax code
function hai(id)
{ 
    $.ajax({ 
    url: "update.php", 
    type: 'POST', 
    data: { 
    "id":id 
    }, 
    success: function (data) 
    { 
    alert(data); 
        // display result 
    } 
    }) 
} 

//on updat.php page 

$indexNo=$_POST['id'];

1 Comment

follow my other question for a detailed outline stackoverflow.com/questions/29389005/…

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.