Page 1 of 1

Replacing GET with POST and auto-filling form fields

Posted: Sun Aug 05, 2007 8:34 am
by trassalg
I've got the following page that is used to retrieve entries from a database and put the values in their respective fields for the user to then edit and UPDATE on the database. It's working but first I have to press Submit, then when that page loads I have to click Edit. Is there a way to avoid this extra step of having to select Edit on the followup page?

Any ideas on how I can get this to work? It doesn't seem like a difficult function to add, but for some reason I'm lost.

Also right now when I want to edit articles the form fields are sent to their respective fields via a GET command, but with large articles this complicates things as URL's have max limit... sometimes not allowing the full article text. Yet I can't seem to get the POST function to work properly on this. Any help?

walkThru_edit.php

Code: Select all

<html>
<head>
<title>Baso de datos de Rodolfo</title>
<link href="styles/cal.css" rel="stylesheet" type="text/css">
<script type="text/javascript"> 
function confirmDelete() { 
 return confirm("¿Estas seguro que lo queres borrar?");   
} 
</script>
<script language="javascript" type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE.init({
    mode : "textareas"
});
</script>
<?php
    include("includes/misc.inc");
    include("includes/connection.inc");
    include("includes/menuBar.php");

            $result = mysql_query("SELECT * FROM articleTable ORDER BY articleIDNumber");
            while ($row = mysql_fetch_array($result)) {
                    }

            $ud_id = $_POST['articleIDNumber'];
            $ud_title = $_POST['articleTitle'];
            $ud_text = $_POST['articleText'];
            $ud_author = $_POST['author'];
                $ud_author2 = $_POST['author2'];
            $ud_source = $_POST['source'];
                $ud_source2 = $_POST['source2'];

?>
</head>
<body>

<table align="center" border="1" width="850">
<form method="post" action="walkThru_update.php">
    <tr>
      <td colspan="4" align="center"><h1>Walk Through articles</h1></td>
  </tr>
  <tr>
      <td colspan="3"></td>
        <td rowspan="8" colspan="2" height="400" width="100">Categories:<br><br><div style="height: 400px; width: 300; overflow: auto"><?php include("categories_edit.php"); ?></div></td>
  </tr>
    <tr>
        <td>Article ID</td>
        <td><input type="text" name="articleIDNumber" value="<?php echo "$articleIDNumber"; ?>"></td>
    </tr>
    <tr>
        <td>Article Date</td>
        <td><input type="text" name="dateOfArticle" value="<?php echo "$dateOfArticle"; ?>"></td>
    </tr>
    <tr>
        <td>Author</td>
            <td><input type="text" name="author2" value="<?php echo "$author"; ?>"><font color="#FF0000"><strong>&nbsp;&nbsp;&nbsp;O&nbsp;&nbsp;&nbsp;</strong></font><?php include("dropdown_author_add.php"); ?></td>
    </tr>
    <tr>
        <td>Source</td>
            <td><input type="text" name="source2" value="<?php echo "$source"; ?>"><font color="#FF0000"><strong>&nbsp;&nbsp;&nbsp;O&nbsp;&nbsp;&nbsp;</strong></font><?php include("dropdown_source_add.php"); ?></td>
    </tr>
    <tr>
        <td>Title</td>
        <td><input type="text" name="articleTitle" value="<?php echo "$articleTitle"; ?>"></td>
    </tr>
    <tr>
        <td>Text</td>
        <td><textarea cols="50" rows="20" input type="text" name="articleText"><?php echo "$articleText"; ?></textarea></td>
    </tr>
  <tr>
    <td></td>
    <td align="center">
            <table>
          <tr>
                    <td width="20%"></td>
                    <td width="20%"></td>
                    <?php 
                include("includes/misc.inc");
                include("includes/connection.inc");
      
             $result = mysql_query("SELECT * FROM articleTable WHERE articleIDNumber=$articleIDNumber ORDER BY articleIDNumber");
              while ($row = mysql_fetch_array($result)) {
            ?></td>
            <td width="20%" align="center"><input type="Submit" value="Submit"></td>
                    <td width="20%"></td>
                    <td width="20%"></td>
              </tr>
        <tr>
          <td width="20%"></td>
          <td width="20%" align="center"><a href="walkThru_edit.php?articleIDNumber=<? echo $row['articleIDNumber']; ?>&dateOfArticle=<? echo $row['dateOfArticle']; ?>&articleTitle=<? echo $row['articleTitle']; ?>&articleText=<? echo $row['articleText']; ?>&author=<? echo $row['author']; ?>&source=<? echo $row['source']; ?>">Edit</a></td>
          <td width="20%"></td>
          <td width="20%" align="center"><a href="walkThru_delete.php?articleIDNumber=<? echo $row['articleIDNumber']; ?>" onClick="return confirmDelete()">Delete</a></td>
          <td width="20%"></td>
        </tr>
      </table>
    </td>
  </tr>
            <?
            } // Closes While Loop
        ?>
</form>
</table>
</body>
</html>
walkThru_update.php

Code: Select all

<html>
<head>
<title>Baso de datos de Rodolfo</title>
<link href="styles/cal.css" rel="stylesheet" type="text/css">
<?php 
    include("includes/misc.inc");
    include("includes/connection.inc");
    include("includes/menuBar.php");
 
      // CHECK POST VALUE EXIST
      if(isset($_POST['categories'])) {
          $categories=$_POST['categories'];
          // IF YES, COUNT array
          $nb_categories=count($categories);
          // MAKE SURE THAT EVERY VALUE OF THE array IS [a-z]
          for ($i=0; $i<$nb_categories; $i++) {
              }
          unset($nb_categories);
          }
      // IF NOT, array NULL
      else {
          $categories=array();
      }
      // Connect information, separated by commas for insertion into database
      $categories_for_db = implode(",", $categories); 

        $ud_id = $_POST['articleIDNumber'];
        $ud_title = $_POST['articleTitle'];
        $ud_text = $_POST['articleText'];
        $ud_author = $_POST['author'];
        $ud_author2 = $_POST['author2'];
        $ud_source = $_POST['source'];
        $ud_source2 = $_POST['source2'];
        $ud_categories = $_POST['categories'];

if ($ud_author == "-----") {
    $ud_author_db = $ud_author2;
    }
    else {
        $ud_author_db = $ud_author;
        }

if ($ud_source == "-----") {
    $ud_source_db = $ud_source2;
    }
    else {
        $ud_source_db = $ud_source;
        }

        $query="UPDATE articleTable SET articleTitle='$ud_title', articleText='$ud_text', categories='$categories_for_db', author='$ud_author_db', source='$ud_source_db' WHERE articleIDNumber='$ud_id'";
        mysql_query($query);
        mysql_close();

?>
<meta http-equiv="refresh" content="1;url=walkThru.php"></head>
<body>
<br><br><br><br><br><br><br><br><br>
<? echo "<div align=center><h3>Actualizado con exito!</h3><br><br><h4>Re-enviandote a la pagina anterior...<blink>|</blink></h4></div>"; ?>
</body>
</html>

Posted: Sun Aug 05, 2007 8:05 pm
by califdon
Your _edit script has a form with a post method and action = your _update script. That's fine. Your _update script doesn't have a form, but sends a refresh header. As far as I know (somebody correct me if I've just overlooked something), that isn't going to create any post variables. Possibly there's some way to send special headers, like I suppose happens in ajax applications. But I'm pretty sure that just an http-equiv refresh won't do it.