2016-06-18 25 views
0

私は私のフォームの次のコードを持っている。しかし、私のprocess/editpage.phpPHP Ajaxのフォーム提出

<?php 
    session_start(); 
    if((!isset($loginpage)) && $_SESSION['logged'] != true){ 
     header('Location: login.php'); 
     die(); 
    } 
    require_once("../../config.php"); 

    if(isset($_POST["updatepagebtn"])){ 
     $updatepage = $db->prepare("UPDATE content SET title=:title, description=:desc, body=:body, status=:status, slug=:url, menutitle=:menutitle, menu=:menu WHERE id=:id"); 

     $updatepage->bindParam(':title', $_POST["title"]); 
     $updatepage->bindParam(':desc', $_POST["description"]); 
     $updatepage->bindParam(':body', $_POST["body"]);  
     $updatepage->bindParam(':status', $_POST["status"]);  
     $updatepage->bindParam(':menutitle', $_POST["menutitle"]); 
     $updatepage->bindParam(':menu', $_POST["menu"]); 
     $updatepage->bindParam(':id', $_POST["id"]); 
     $url = "/".$_POST["url"]; 
     $updatepage->bindParam(':url', $url); 

     if($updatepage->execute()){ 
      echo "Success!"; 
     }else{ 
      echo "Error updating page. Ensure you have entered a valid and unique URL.";    
     } 
    }else{ 
     echo "Error"; 
    } 

ため

$('#updatepagebtn').click(function() { 
    $(this).preventDefault(); 
    $.ajax({ 
     type:"post", 
     url:"process/editpage.php", 
     data: $("#editpageform").serialize(), 
     success: function(response){ 
      $(".result").html(response); 
     } 
    }); 
}); 

そして、この:私のJSのため

<form id="editpageform" method="post"> 
      <div class="form-group"> 
       <label for="title">Page Title</label> 
       <input type="text" id="title" name="title" placeholder="Page Title" value="<?php echo $pageRes["title"];?>" class="form-control" required> 
      </div> 
      <div class="form-group"> 
       <label for="description">Page Description (Max 160 Characters)</label> 
       <textarea id="description" rows="4" class="form-control" name="description"><?php echo $pageRes["description"];?></textarea> 
       <span id="remain_desc"><?php echo 160 - strlen($pageRes["description"]);?></span> Characters Remaining 
      </div> 
      <div class="form-group"> 
       <label for="body">Page Content</label> 
       <textarea name="body" class="form-control tinymce"><?php echo $pageRes["body"];?></textarea> 
      </div> 
      <div class="form-group"> 
       <label for="status">Page Status</label> 
       <select id="status" class="form-control" name="status"> 
        <option value="Draft" <?php echo ($pageRes["status"] == "Draft") ? "selected" : "" ?>>Draft</option> 
        <option value="Published" <?php echo ($pageRes["status"] == "Published") ? "selected" : "" ?>>Published</option> 
       </select> 
      </div> 
      <div class="form-group"> 
       <label for="url">Page URL</label> 
       <div class="input-group"> 
        <span class="input-group-addon" id="basic-addon2">http://www.champpaw.co.uk/</span> 
        <input type="text" name="url" id="url" value="<?php echo substr($pageRes["slug"], 1);?>" class="form-control"> 
       </div> 
      </div> 
      <div class="form-group"> 
       <label for="menutitle">Menu Title</label> 
       <input type="text" name="menutitle" id="menutitle" class="form-control" value="<?php echo $pageRes["menutitle"];?>"> 
      </div> 
      <div class="form-group"> 
       <label for="menutitle">Menu Order</label> 
       <input type="number" name="menu" id="menu" class="form-control" value="<?php echo $pageRes["menu"];?>"> 
      </div> 
      <div class="form-group"> 
       <input type="hidden" readonly id="id" name="id" value="<?php echo $pageRes["id"];?>"> 
       <button class="btn btn-default" name="updatepagebtn" id="updatepagebtn">Update Page</button> 
      </div> 
     </form> 

そして、これをフォームを送信してテーブルを更新するのではなく、読み込んだフォームを送信します。私はaction="process/editpage.phpをフォームに追加しようとしましたが、うまくいきましたので、問題はjQueryにあると思いますが、それが何であるかわかりません。

誰もが問題を見て、それが何であるかを私に教えてもらえますか?

おかげ

+0

コンソールログには何もfunction(e) { e.preventDefault()

$(document).ready(function() { $('#updatepagebtn').click(function(e) { $.ajax({ type: "post", url: "process/editpage.php", data: $("#editpageform").serialize(), success: function(response) { $(".result").html(response); } }); e.preventDefault(); }); }); 

をコードfunction(e) { $(this).preventDefault()を変更また$(document).ready(function(){ の内側にあなたのjQueryのコードをラップ? – chris85

+0

'#updatepagebtn'の' click'イベントの代わりに '#editpageform'で' submit'イベントを試みるかもしれません – Rasclatt

+0

'... function(e){ e.preventDefault(); ...' – Rasclatt

答えて

0

は別の方法

$(document).ready(function() { 
    $('#editpageform').submit(function(e) { 
    $.ajax({ 
     type: "post", 
     url: "process/editpage.php", 
     data: $("#editpageform").serialize(), 
     success: function(response) { 
     $(".result").html(response); 
     } 
    }); 
    e.preventDefault(); 
    }); 
}); 
関連する問題