2015-01-31 60 views
1

我目前正在研究一個PHP CMS,並試圖爲新聞系統創建一個編輯頁面。單個php文件中的多個表單

當用戶轉到編輯頁面並且文章尚未被選中時,他們應該得到一個表單來選擇要編輯的文章。

當用戶選擇要編輯的文章時,頁面應該(刷新?)顯示一個表單,其中包含要編輯的所選文章內容。


我的所有頁面都包含在我的index.php中,基於p = pagename被設置。

當我去編輯新聞頁面,我看到http://puu.sh/fhYO7/569146d037.png(這是正確的)。

當我選擇一個新聞文章編輯發送我http://puu.sh/fhZ13/2ca0f7d974.png與網址爲:mysite.com/admin/editnews.php?id=4

如果我手動輸入網址應該是什麼:mysite的?.COM /管理/ index.php的p = editnews & ID = 4我得到這個 - http://puu.sh/fhZ6h/d38e97d0bb.png

我當前的代碼: editnews.php

<?php 
    ob_start(); 
    session_start(); 

    include_once('includes/connection.php'); 
    include_once('includes/news.php'); 

    $news = new AdminNews; 

    if (isset($_SESSION['logged_in'])) { 
     $newss = $news->fetch_all(); 

     if (isset($_POST['title'], $_POST['content'])) { 
      $title = $_POST['title']; 
      $content = br2nl($_POST['content']); 

      if (empty($title) or empty($content)) { 
       $error = 'All fields are required!'; 
       header('Location: index.php?p=editnews'); 
      } else { 
       global $pdo; 

       $query = $pdo->prepare('UPDATE admin_news SET news_title = ?, news_content = ? WHERE news_id=?'); 
       $query->bindValue(1, $title); 
       $query->bindValue(2, $content); 
       $query->bindValue(3, $id); 

       $query->execute(); 

       header('Location: index.php'); 
      } 
     } 

     if (isset($_GET['id'])) { 
      $id = $_GET['id']; 
    ?> 
    <!-- POST --> 
    <div class="post"> 
     <div class="topwrap"> 
      <div class="userinfo pull-left"> 
       <div class="avatar"> 
        <img src="images/avatar.jpg" alt="" /> 
        <div class="status green">&nbsp;</div> 
       </div> 

       <div class="icons"> 
        <img src="images/icon1.jpg" alt="" /><img src="images/icon4.jpg" alt="" /><img src="images/icon5.jpg" alt="" /><img src="images/icon6.jpg" alt="" /> 
       </div> 
      </div> 
      <div class="posttext pull-left"> 
       <h2>Edit News</h2> 
       <?php if (isset($error)) { ?> 
        <small><?php echo $error; ?></small> 
       <?php } ?> 
       <!-- add news form start !--> 
       <form action="editnews.php" method="post" autocomplete="off"> 
        <input type="text" name="title" value="<?php echo $news['news_title'] ?> /><br /><br /> 
        <textarea rows="10" cols="87" value="<?php echo $news['news_content'] ?>" name="content" /></textarea> 
        <!-- add news form break !--> 
      </div> 
      <div class="clearfix"></div> 
     </div>        
     <div class="postinfobot"> 

      <div class="dateposted pull-right"> 
        <!-- add news form continue !--> 
        <input class="btn btn-primary" type="submit" value="Submit Changes" /> 
       </form> 
       <!-- add news form end !--> 
      </div> 

      <div class="clearfix"></div> 
     </div> 
    </div> 
    <!-- POST --> 
    <?php 
     } else { 
    ?> 
    <!-- POST --> 
    <div class="post"> 
     <div class="topwrap"> 
      <div class="userinfo pull-left"> 

      </div> 
      <div class="posttext pull-left"> 
       <h2>Select a News Post to Delete</h2> 
       <!-- form start !--> 
       <form action="editnews.php" method="get" autocomplete="off"> 
        <select name="id"> 
         <?php foreach ($newss as $news) { ?> 
          <option value="<?php echo $news['news_id']; ?>"> 
           <?php echo $news['news_title']; ?> 
          </option> 
         <?php } ?> 
        </select> 
        <!-- form break !--> 
      </div> 
      <div class="clearfix"></div> 
     </div>        
     <div class="postinfobot"> 
      <div class="dateposted pull-right"> 
       <!-- form continue !--> 
       <input class="btn btn-primary" type="submit" value="Edit News" /> 
       </form> 
       <!-- form end !--> 
      </div> 

      <div class="clearfix"></div> 
     </div> 
    </div> 
    <!-- POST --> 
    <?php 
     } 
    } else { 
     header('Location: index.php'); 
    } 

    ?> 

我完全失去了在這一個(j 10年後,ust開始回到PHP)。

的問題是: 什麼我做錯了,爲什麼不是它給我正確的URL,它爲什麼顯示奇怪,當我輸入網址應該是什麼?

+0

那麼,什麼是您的具體問題 – 2015-01-31 19:46:51

+1

什麼我做錯了,爲什麼不是它給我正確的URL,它爲什麼顯示奇怪,當我輸入網址應該是我有什麼?一直玩弄這幾個小時,並沒有能夠解決任何問題這是錯誤的。 – Meta 2015-01-31 19:47:45

回答

2

你有兩種選擇:
1.獲取表單發佈到index.php而不是editnews.php並照顧index.php中的數據。換句話說改變:

<form action="editnews.php" method="post" autocomplete="off"> 

到:

<form action="index.php" method="post" autocomplete="off"> 
  • 提交表單與AJAX沒有頁面刷新。這意味着你必須處理JavaScript中的按鈕點擊處理與一個Ajax調用的形式網址editnews.php
  • jQuery中這是可以做到像這樣:

    $("#myForm").submit(function() { 
        $.post('editnews.php', 
        { 
         title: $('#name').val(), 
         content: $('#content').val() 
        }, 
        function(data) { 
         console.log(data); //response 
         $('#name, #content').val(''); /* Clear the inputs */ 
        }, 
        'text'); 
        return false; //Stop form from refreshing page 
    }); 
    
    1

    你應該嘗試在開始時用斜線表示:

    header('Location: /index.php?p=editnews'); 
    

    乾杯!

    0

    我終於想出了一個解決方案(只用了FOREVER)...

    首先我所做的是刪除新聞選擇形式和剛滿選擇進入鏈接標題的列表會帶你到& id =#的正確鏈接。

    然後我改變了檢查,看是否該ID設置與否,如果是這樣顯示的形式編輯(形式顯示錯了,因爲有一個缺失「和;在我的形式

    固定碼:

    <?php 
    ob_start(); 
    session_start(); 
    
    include_once('includes/connection.php'); 
    include_once('includes/news.php'); 
    include_once('includes/functions.php'); 
    
    $news = new AdminNews; 
    $funct = new UserFunctions; 
    
    if (isset($_SESSION['logged_in'])) { 
        $newss = $news->fetch_all(); 
    
        if (isset($_POST['title'], $_POST['content'])) { 
         $title = $_POST['title']; 
         $content = nl2br($_POST['content']); 
    
         if (empty($title) or empty($content)) { 
          $error = 'All fields are required!'; 
          header('Location: index.php?p=editnews'); 
         } else { 
          global $pdo; 
    
          $query = $pdo->prepare('UPDATE admin_news SET news_title = ?, news_content = ? WHERE news_id=?'); 
          $query->bindValue(1, $title); 
          $query->bindValue(2, $content); 
          $query->bindValue(3, $id); 
    
          $query->execute(); 
    
          header('Location: index.php'); 
         } 
        } 
    
        if (isset($_GET['id'])) { 
         $id = $_GET['id']; 
    
         $query = $pdo->prepare("SELECT * FROM admin_news WHERE news_id = ?"); 
         $query->bindValue(1, $id); 
         $query->execute(); 
    
         $rows = $query->fetchAll(); 
    
         //set username to variable 
         foreach ($rows as $row) { 
          $id = $row['news_id']; 
          $title = $row['news_title']; 
          $content = $funct->br2nl($row['news_content']); 
         } 
    ?> 
    <!-- POST --> 
    <div class="post"> 
        <div class="topwrap"> 
         <div class="userinfo pull-left"> 
          <div class="avatar"> 
           <img src="images/avatar.jpg" alt="" /> 
           <div class="status green">&nbsp;</div> 
          </div> 
    
          <div class="icons"> 
           <img src="images/icon1.jpg" alt="" /><img src="images/icon4.jpg" alt="" /><img src="images/icon5.jpg" alt="" /><img src="images/icon6.jpg" alt="" /> 
          </div> 
         </div> 
         <div class="posttext pull-left"> 
          <h2>Edit News</h2> 
          <!-- add news form start !--> 
          <form action="editnews.php" method="post" autocomplete="off"> 
           <input type="text" name="title" value="<?php echo $title; ?>" /><br /><br /> 
           <textarea rows="10" cols="87" name="content" /><?php echo $content; ?></textarea> 
           <!-- add news form break !--> 
         </div> 
         <div class="clearfix"></div> 
        </div>        
        <div class="postinfobot"> 
    
         <div class="dateposted pull-right"> 
           <!-- add news form continue !--> 
           <input class="btn btn-primary" type="submit" value="Submit Changes" /> 
          </form> 
          <!-- add news form end !--> 
         </div> 
    
         <div class="clearfix"></div> 
        </div> 
    </div> 
    <!-- POST --> 
    <?php 
        } else { 
    ?> 
    <!-- POST --> 
    <div class="post"> 
        <div class="topwrap"> 
         <div class="userinfo pull-left"> 
    
         </div> 
         <div class="posttext pull-left"> 
          <h2>Select a News Post to Delete</h2> 
            <?php foreach ($newss as $news) { ?> 
             <?php echo $news['news_id']; ?> - <a href="index.php?p=editnews&id=<?php echo $news['news_id']; ?>"><?php echo $news['news_title']; ?></a><br /><br /> 
            <?php } ?> 
         </div> 
         <div class="clearfix"></div> 
        </div>        
        <div class="postinfobot"> 
         <div class="dateposted pull-right"> </div> 
    
         <div class="clearfix"></div> 
        </div> 
    </div> 
    <!-- POST --> 
    <?php 
        } 
    } else { 
        header('Location: index.php'); 
    } 
    
    ?>