SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: $POST help

  1. #1
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $POST help

    Code:
    	  <form action="" method="post">
    <table width="500" border="0" align="center">
      <tr>
        <th scope="row">Title: </th>
        <td><div align="center">
          <input name='title' type='text' value="<? echo("$title");?>">
        </div></td>
      </tr>
      <tr>
        <th scope="row">Subtext:</th>
        <td><div align="center">
          <input name='subtext' type='text' value="<? echo("$subtext");?>">
        </div></td>
      </tr>
      <tr>
        <th scope="row">&nbsp;</th>
        <td><div align="center"></div></td>
      </tr>
      <tr>
        <th scope="row">&nbsp;</th>
        <td><div align="center"></div></td>
      </tr>
      <tr>
        <th scope="row">&nbsp;</th>
        <td><div align="center"></div></td>
      </tr>
      <tr>
        <th scope="row">&nbsp;</th>
        <td><div align="center"></div></td>
      </tr>
      <tr>
        <th scope="row">&nbsp;</th>
        <td><div align="center"></div></td>
      </tr>
      <tr>
        <th scope="row">&nbsp;</th>
        <td><div align="center"></div></td>
      </tr>
      <tr>
        <th scope="row">&nbsp;</th>
        <td><div align="center"></div></td>
      </tr>
      <tr>
        <th scope="row">&nbsp;</th>
        <td><div align="center"></div></td>
      </tr>
    </table>
    
    <div align="center">
    <input value='Save Changes' type='submit'></form>
    
    <?php
    //Don't edit this--
    $bad = array("<", ">", '"', "'",";");
    $good  = array(" ", " "," ", " ", " ");
    //Don't edit this--end
    
    //add or modify the variables to change using similar format here
    $title = $_POST['title'];
    $subtext = $_POST['subtext'];
    
    //view this format and modify to suit more or less variables - this stage cleans the user input
    $cleantitle = str_replace($bad, $good, $title);
    $cleansubtext = str_replace($bad, $good, $subtext);
    
    //heres the new file contents, again look at the format and simply copy & paste to suite more or less variables
    $content_to_write = 
    '<?php 
    $title = "' . $cleantitle . ' ";
    $subtext = "' . $cleansubtext . ' ";
    ?>';
    
    //filename of the file to hold variables
    $filename = '../inc/variables.php';
    
    // Main PHP
    if (is_writable($filename)) {
    
       if (!$handle = fopen($filename, 'w+')) {
             echo "Cannot open file ($filename)";
             exit;
       }
    
       if (fwrite($handle, $content_to_write) === FALSE) {
           echo "Cannot write to file ($filename)";
           exit;
       }
      
       // echo "Success, variables changed!";
      
       fclose($handle);
    
    } else {
       echo "The file $filename is not writable";
    }
    
    //Heres the form HTML that you need to add / modify to change the variables edited, note that $_POST['key'] matches up to name='key'
    
    ?>
    So I have $title = $_POST['title']; and I only want title to be posted/changed when the form button is clicked. Whenever I refresh this page, it $_POST's title.

    I was thinking of using something like if $title = anyformvalue - but I don't know how to write that. Can someone please give me some reference or help?

    I've been trying to fix this for quite some time now... have tried my best,

    regards

    CWN

  2. #2
    SitePoint Guru hifigrafix's Avatar
    Join Date
    Oct 2005
    Location
    Nashville, TN
    Posts
    628
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try re-phrasing your question a little better. I'm confused on what you're looking for (and the code doesn't help much) but would love to help.

  3. #3
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, when I refresh my page, the variable $title resets to nothing. Can anyone please help?

  4. #4
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think there is something you aren't telling us.

    What assigns something to this <? echo("$title");?> ?

    Have you upgraded your server lately? Try switching that to $_GET['title'] and $_POST['title'] instead of $title and see what happens.
    Upgrading to Mysql 5? Auto-increment fields now strict
    use NULL
    Or zero or leave the field name out completely.

  5. #5
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    The problem is you're echoing $title out before it's actually being assigned a value.
    PHP scripts are run top-down. If you want to have $title echo out, make sure the $title = $_POST['title']; comes before the <? echo("$title");?>.

    Off Topic:


    It's also better to use full php tags instead of short ones:

    <?php ?> == good
    <? ?> == bad



  6. #6
    SitePoint Zealot Wumble's Avatar
    Join Date
    Jun 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Immerse - $title = $_POST['title']; does come out before What else could I do to fix this? Thanks for tip about php tags

    GET didn't change anything. Like, I have the variable set, and when I open the PHP page the variable posts nothing right away. After I post using the button the variable will show in the box... until the page gets refreshed again.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •