如何将Id传递给ajax中的新页面

时间:2013-12-24 11:35:50

标签: javascript php jquery html ajax

通过其ID获取评论的Javascript所有的javascript都可以在这里访问  here

//get a review by id
function Getreviewid(id){
    $.ajax({
        url: 'http://creative.coventry.ac.uk/~4078078/moviereviews/v1.0/index.php/review/reviewid/'+id,
        dataType: 'json',
        success: function(data) {
    console.log(id);
    $("#reviewdetail").empty();
        $('#reviewdetail').append('<img src='+data.review.image+' style="width:150px; height:150px" , onerror="imgError(this);"</img>');
    $('#reviewdetail').append('<p><b>Title: </b>'+data.review.Title+'</p>');
    $('#reviewdetail').append('<p><b>Publisher: </b> '+data.review.publisher+'</p>');
    $('#reviewdetail').append('<p><b>Review: </b> '+data.review.review+'</p>');
  // $('#reviewdetails').append('<a href="#updatereview" id="editreview" onclick="updatereview('+review.id+');" data-role="button" data-mini="true" data-inline="true">Edit</a>');
    $.mobile.changePage("#detailreview"); //show the results page
    },

error: function (response) {
        var r = jQuery.parseJSON(response.responseText);
        alert("Message: " +r.error.text);

    }

function updateReviewForm(){
    id =$("#review-id").text();
   $("#r_id").val = id;
}

function updatereview(id){

$.ajax({
type:'post',
    url: 'http://creative.coventry.ac.uk/~4078078/moviereviews/v1.0/index.php/review/updatereview/'+id,
headers: {authorization:readCookie('cookie')}, //use the cookie for authentication
data: {
title:$("#updatetitle").val(),
review:$("#updatereviewwrite").val(),
publisher:$("#updatepublisher").val(),
username:$("#updateusername").val(),},
//rating:$("#rating-1").val()
    dataType: 'json',
    success: function(data) {
        alert("Review Updated. Please continue.");
        $("#updatetitle").val(''); //clear the text boxes
        $("#updatereviewwrite").val('');
        $("#updateusername").val('');
        $("#updatepublisher").val('');
        //$("#rating-1").val('');
        Getreviewid(reviewsisbn);


    },
error: function (response) {
    var r = jQuery.parseJSON(response.responseText);
    console.log(r);
    alert("Error:" + r.error.text);
               }
        });

}

更新表单的HTML代码:

<!--update review-->
<div data-role="page" id="updatereview" data-theme="a">
<div data-theme="a" data-role="header">
     <a data-role="button" data-mini="true" data-rel="back" data-theme="a"  data-icon="back"
    data-iconpos="notext" data-transition="slide"></a>
    <h3 class="pagetitle">
      Write a Review
    </h3>
</div>
<div data-role="content">

      <h3>Update a Review</h3>
        <fieldset>
            <div data-role="fieldcontain">
                <label for="updatetitle">Title:</label>
             <textarea type="text" value=""  name="updateTitle" id="updatetitle"></textarea>
                        </div>
            <div data-role="fieldcontain">
                <label for="updatereviewwrite">Review:</label>
             <textarea type="text" value="" name="updatereviewwrite" id="updatereviewwrite"></textarea>
                        </div>
            <div data-role="fieldcontain">
                <label for="updatepublisher">Publisher:</label>
             <textarea type="text" value="" name="updatepublisher" id="updatepublisher"></textarea>
                        </div>
            <div data-role="fieldcontain">
                <label for="updateusername">Username:</label>
             <textarea type="text" value="" name="updateusername" id="updateusername"></textarea>
                        </div>
                            <!-- <div id="rating"></div> <br />
                    Your Rating: <span id="rating-1">not set</span>-->
                    <input type="hidden" value="" id="r_id" name="r_id">
            <input type="button" data-theme="a" name="submit" onclick="updatereview()" value="Submit" >

        </fieldset>
</div>
  </div>

显示评论详情的页面的HTML代码

    <!--detail Reviews example page-->
<div data-role="page" id="detailreview" data-theme="a">
    <div data-theme="a" data-role="header">
         <a data-role="button" data-mini="true" data-rel="back" data-theme="a"  data-icon="back"
        data-iconpos="notext" data-transition="slide"></a>
        <h3 class="pagetitle">
            Reviews
        </h3>
    </div>
    <div data-role="content">
    <div id="reviewdetail">
      <!--get review details-->
    </div>
     <a data-role="button" id="editreview" href="#updatereview" onclick="updateReviewForm()" data-mini="true"  data-theme="a"  data-icon="edit"
         data-transition="slide">Edit</a>
          <div data-theme="a" data-role="navbar" data-iconpos="bottom" data-position="fixed" >

                               <ul>
                <li>
                    <a href="#home" data-transition="slide" data-theme="a" data-icon="home" > Home</a>
                </li>

                <li>
                    <a onclick="Getreviewmovies()" data-transition="slide" data-theme="a" data-icon="edit" >
                        Reviews
                    </a>
                </li>

                  <li>
                    <a href="#contactus" data-rel="dialog" data-transition="pop" data-theme="a" data-icon="bars">
                      Contact us
                    </a>
                </li>


            </ul>
         </div>
        </div>

      </div>

用于更新评论的PHP代码如下

function updatereview_post($id=null){
{   
        $headers = apache_request_headers();
        {   
            if (empty($headers['authorization'])) {
               $info->status = 'failure';
               $info->error->code = 15;
                $info->error->text = 'basic HTTP AUTHENTICATION required';
                $this->response($info, 401);
         }
        }
    $string = base64_decode($headers['authorization']);

        list($username, $password) = explode(':', $string);

        {   
            $this->load->database();
            $sql  = 'SELECT COUNT(userid) AS records FROM users '.'WHERE authorization = "'.$headers['authorization'].'";';

            $query = $this->db->query($sql);
            $data = $query->row();
            if ($data->records == "0") {
                $info->status = 'failure';
                $info->error->code = 19;
                $info->error->text = 'authorization string is not valid';
                $this->response($info, 401);
            }
    }}
    {
if (!isset($id)){// check if the ID is specified in the URI
    $info->status = 'failure';
            $info->error->code = 11;
            $info->error->text = 'id not specified in URI';
            $this->response($info, 400);
}}
 {  // if the resource exist
        $this->load->database();
        $sql = 'SELECT COUNT(id) AS records FROM reviews WHERE id = '.$id.';';
        //$this->response($sql, 200);
        $query = $this->db->query($sql);
        $data = $query->row();
        if ($data->records == "0") {
            $info->status = 'failure';
            $info->error->code = 12;
            $info->error->text = 'id does not exist or have a resource';
            $this->response($info, 404);
        }
 }
$this->load->database();
$sql= 'SELECT * FROM reviews WHERE id= "'.$id.'";';
$query= $this->db->query($sql);
$data->old = $query->row();
$data->changes = $_POST;
$this->db->where('id', $id);
$this->db->set('reviews',$_POST);
$data->rows = $this->db->affected_rows();
$query= $this->db->query($sql);
$data->new = $query->row();
$this->response($data,200);
}

1 个答案:

答案 0 :(得分:0)

您的评论详细信息和更新表单应位于同一页面以便在代码下方工作。 看来在你的Getreviewid js函数中你需要添加这段代码:

success: function(data) {
    console.log(id);
    $("#reviewdetail").empty();
        $('#reviewdetail').append('<img src='+data.review.image+' style="width:150px; height:150px" , onerror="imgError(this);"</img>');
    $('#reviewdetail').append('<p><b>Title: </b>'+data.review.Title+'</p>');
    $('#reviewdetail').append('<p><b>Publisher: </b> '+data.review.publisher+'</p>');
    $('#reviewdetail').append('<p><b>Review: </b> '+data.review.review+'</p>');
    $("#r_id").val( data.review.id); // set review id for update form
    $.mobile.changePage("#detailreview"); //show the results page
    }

您不需要updateReviewForm函数。在updatereview函数中添加以下行:

data: {
review_id:  $("#r_id").val(),

在此之后,您可以在php页面中获取评论ID,如下所示:

  function updatereview_post($id=null){
     $id = $_REQUEST['review_id'];

我认为现在有道理:)