duan0530 2016-06-18 14:38
浏览 31
已采纳

PHP Ajax表单提交

I have the following code for my form:

<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>

And this for my JS:

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

And this for my process/editpage.php

<?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";
    }

However when I submit the form it just seems to reload instead of submitting the form and updating the table. I tried adding action="process/editpage.php to the form and it worked fine then so I think the problem is with the jQuery but I am not sure what it is.

Can anyone see the problem and let me know what it is so I can get it fixed?

Thanks

  • 写回答

1条回答 默认 最新

  • dtpd58676 2016-06-18 14:56
    关注

    Wrap your jQuery code inside $(document).ready(function(){ Also change code function(e) { $(this).preventDefault() to 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();
      });
    });
    

    Another way

    $(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();
      });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用