0

i want to replace result from readmore.php into <div id='box'> base on id_pages at index.php after click
<a class=readmore> Read More </a>.
i tired to try how to get value from readmore.php using ajax.

this's my code

index.php

<html>
    <head>
        <link rel='stylesheet' type='text/css' href='style.css'>
        <title> Home Page </title>

<script src="//code.jquery.com/jquery-1.10.2.js"></script>



<script>
$('.readmore').click(function(){    
    $.ajax({
        type: "POST",
        url: "readmore.php",
        data: dataString,
        success: function(returnedData) {
        $( '#daleman' ).html(returnedData);
        }
    });
})

</script>

    </head>

    <body>

<?php 
error_reporting(E_ERROR | E_PARSE);

include "koneks.php";

$db = new database;

$db->connectMYSQL();

//* Memulai Tampilan Menu
echo"
<div id='menu'>
    <a href='manage.php'>Manage Blog</a>
</div>
";

//*  Memulai Tampilan Page
$arraypage = $db->tampilPage();

foreach($arraypage as $data) {
echo"
<input type=hidden id=idnya name='idnya' value=".$data['id_pages'].">
<div id='boxpage'>
    <div id='alasatas'>
    </div>

    <div id='alasjudul'>
    ".$data['judul']." 
    </div>

    <div>
        <div id='alasfoto'>
        <img height='200px' src='pict/".$data['foto']."'>
        </div>

        <div id='alasisi'>
            <div id='daleman'>
            <br>

            ".$data['deskripsi']."

            </div>

        </div>
    </div>

    <div id='alasketerangan'>
    <center>Tanggal dibuat : ".$data['tgl_dibuat']." &nbsp; &nbsp; &nbsp; Label : ".$data['label']." &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class='readmore' >Read More</a> </center>
    </div>
</div>

</body>
</html>
";
}

?>

this my readmore.php

<?php

    include "koneks.php";

    $dbi = new database;

    $dbi->connectMYSQL();

    echo"
    $_POST[idnya]
    ";

?>

i need help meybe to fix my code, my thanks

13
  • use data instead of returnedData. Commented Feb 4, 2015 at 7:18
  • @SunilPachlangia for the example sir ? please Commented Feb 4, 2015 at 7:20
  • 1
    you are using same id "box" in for loop this will not work.. Commented Feb 4, 2015 at 7:21
  • At this line $('#box').html(returnedData); just replace returnedData with data Commented Feb 4, 2015 at 7:22
  • and what's in dataString? Commented Feb 4, 2015 at 7:22

3 Answers 3

1

There are certain things missing in the code you've posted. Lets just simplify the code a little bit with some modifications.

index.php

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script> <!-- Added jQuery Reference -->
</head>
<body> <!-- Added body tag -->
<?php
echo "<input type='hidden' id='idnya' value='value_posted_for_idnya'><div id='box'> Replace Me </div> <a class='readmore' href='javascript:void(0)'>ReadMore</a>";
?>
<script>
$(document).ready(function(){ // Wrapped the script in $(document).ready function
$('.readmore').click(function(){    
    $.ajax({
        type: "POST",
        url: "readmore.php",
        data: "idnya=" + $("#idnya").val(), //say you have only one field to post for now
        success: function(returnedData) {
        $('#box').html(returnedData);
        }
    });
})});
</script>

</body>
</html> 

readmore.php

Can stay the same.

Try the code above and see what you get.

EDIT

If you have multiple fields to post with ajax function you can use serializeArray() method.

Simply wrap your fields with in a form tag as follows

<form id="frm">
...form controls
</form>

This time you have to specify name attributes in order for serializeArray() to work.

Then your new ajax function becomes

$(document).ready(function(){ 
$('.readmore').click(function(){    
    $.ajax({
        type: "POST",
        url: "readmore.php",
        data: $("#frm").serializeArray(), //get all name-value pairs within form
        success: function(returnedData) {
        $('#box').html(returnedData);
        }
    });
})});
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

you're welcome. I've also updated the answer for multiple fields to post
0

its possible that click function is not executing beacause you dont prevent default anchor action

<script>
$('.readmore').click(function(event){  
    event.preventDefault();
    $.ajax({
        type: "POST",
        url: "readmore.php",
        data: dataString,
        success: function(returnedData) {
        $('#box').html(returnedData);
        }
    });
})
 </script>

Comments

0

I have modified your code a little try doing this:

<script>
$('.readmore').click(function(){    
    var idnya = $('#idnya').val();
    $.ajax({
        type: "POST",
        url: "readmore.php?idnya="+idnya,
        data: dataString,
        success: function(returnedData) {
        $('#box').html(returnedData);
        }
    });
})
 </script>

and in your readmore.php file use $_GET['idnya'] to get this value and use it in your query.

Comments

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.