Passing dynamic data from a div to another div on the same page

I'm stuck in a problem and need support from you guys.

My problem is that I want to pass the php variable (dynamically called through the database in a loop) to another div on the same page without refreshing the page

// This is a loop

<td><a class="linkright" href="#existingcase?case_id=<?php echo $row_mycases['case_id']; ?>"><?php echo $row_mycases['case_id']; ?></a></td>

// div that should get the above php variable

<div class="tabright" id="existingcase">
    <?php 
    $c_id = $_GET['case_id'];
    echo $c_id;
    ?>      
</div>

// javascript code to call divs on the same page

<script>
$(".linkright").click(function(){
    $(".tabright").hide();
    theDiv = $(this).attr("href");
    $(theDiv).slideToggle();
});
</script>

It appears in a URL like this index.php#existingcase?case_id=2012001, but never passes the case identifier to #existingcase. And also the #existingcasediv does not load, but without passing the caseid value, it #existingcaseloads.

Any suggestions would be great.

+4
source share
3

, , case_id div . , URL-. , javascript.

div id existingcase , .

case_id , $row_mycases['case_id'];. , javascript, #existingcase div.

<td><a class="linkright" href="#existingcase"><?php echo $row_mycases['case_id']; ?></a></td>

. .linkright ,

$(".linkright").click(function(){
    $('#existingcase').text($(this).text()); //if your div id is existingcase it will print your case id. 
});

. .

FIDDLE DEMO

:

, jquery.post().

PHP .

<?php     
    if (isset($_POST['id'])) {
    $caseid = $_POST['id'];
    return print_r($caseid);
  }
?>

$caseid case_id. , $caseid , . , case_id js ,

$(".linkright").click(function () {
    $(".tabright").hide();
    theDiv = $(this).attr("href");
    $(theDiv).slideToggle();
    $.post("yourPHPFile.php", {    //use your current php file name instead of "yourPHPFile.php"
        id: $(this).text()
    }, function (caseid) {
        $('#existingcase').text(caseid);
    });
});

id : $(this).text() , .text() $_POST id. yourPHPFile.php. , $caseid = $_POST['id'];.

function (caseid) { caseid $caseid. , $caseid = $_POST['id'];

clicked case_id #exixtingcase div.

+2

# . PHP , , php-, #

<a class="linkright" href="#existingcase?case_id=<?php echo $row_mycases['case_id']; ?>">

<a class="linkright" href="existingcase?case_id=<?php echo $row_mycases['case_id']; ?>">

php- div.

+1

, . Javascript PHP . , PHP . href , PHP- div . PHP- . " " . , - AJAX Javascript, , ( ).

,

EDIT: AJAX JQuery. - :

<td>
    <a class="linkright" href="#" data-id="<?php echo $row_mycases['case_id']; ?>"><?php echo $row_mycases['case_id']; ?></a>
</td>

You will notice that I have added the "data-id" attribute. We will use this identifier in our AJAX call. Put this in your JS:

// Create Jquery ajax request
$("a.linkright").click( function() {
        $.get("example.php",   // Put your php page here
              { case_id : $(this).attr("data-id") }, // Get data, taken from the a tag
              function(data) {  // Call back function displays the response
                   $(".tabright").html("<p>" + data + "</p>");
          }); // Get 
    }); // click event
}); // Document Ready

I tested the code and it works. You will need to configure it to work for you. If all this AJAX is new to you, I offer you the following links:

http://www.w3schools.com/ajax/default.ASP

http://learn.jquery.com/ajax/

0
source

Source: https://habr.com/ru/post/1534002/


All Articles