Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Save form script. Problem

  1. #1
    Join Date
    Jun 2007
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Save form script. Problem

    I am a beginner and building a form processor script that is intended to make a backup of the submitted form, in html format. I might be using the wrong technique, but here is a simple setup of my processor script (writeform.php). My problem is the checkboxes...how can I reproduce a checked checkbox within the $stringData, which depends on if the submitted checkbox is checked?

    <?php
    $file = fopen("formsaved.html", "w") or exit("Unable to open file!");

    $stringData =

    '<head>
    </head>

    <form id="form1" name="form1" method="post" action="writeform.php">
    <input type="text" name="1" id="1" value="'.$_POST['1'].'"/>
    <br />
    <input type="text" name="2" id="2"/>
    <br />
    <input type="checkbox" name="check" id="check" />
    <br />
    <input type="submit" name="button" id="button" value="Save" />
    </form>

    <body>
    </html>
    '
    ;
    fwrite($file, $stringData);
    fclose($file);

    ?>

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    PHP Code:
    $ifchecked ''//initialize to blank
    if ($_POST['check'] == 1$ifchecked 'checked ';
    '.....<input type="checkbox" name="check" id="check" '.$ifchecked.'/>.....' 
    That will add ... checked /> to the checkbox if the value of the submitted checkbox was 1 (checked). Or, it will just leave as is.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jun 2007
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    PHP Code:
    $ifchecked ''//initialize to blank
    if ($_POST['check'] == 1$ifchecked 'checked ';
    '.....<input type="checkbox" name="check" id="check" '.$ifchecked.'/>.....' 
    That will add ... checked /> to the checkbox if the value of the submitted checkbox was 1 (checked). Or, it will just leave as is.
    Perfect thanks. Added a small adjustment so that it would work (the checked=). Next question...say I have 50 checkboxes in a form...is there an easier way to perform the check than having to setup a $ifchecked1, $ifchecked2, ... $ifchecked50?


    CURRENT STATE
    <?php
    $file = fopen("formsaved.html", "w") or exit("Unable to open file!");

    $ifchecked = ''; //initialize to blank
    if ($_POST['check'] == 1) $ifchecked = 'checked ';

    $stringData =

    '<head>
    </head>

    <form id="form1" name="form1" method="post" action="writeform.php">
    <input type="text" name="1" id="1" value="'.$_POST['1'].'"/>
    <br />
    <input type="text" name="2" id="2"/>
    <br />
    <input type="checkbox" name="check" id="check" checked="'.$ifchecked.'"/>
    <br />
    <input type="submit" name="button" id="button" value="Save" />
    </form>

    <body>
    </html>
    '





    ;
    fwrite($file, $stringData);
    fclose($file);

    ?>

  4. #4
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Try this:
    PHP Code:
    $ifchecked = array();
    $checkboxes '';
    for (
    $i=1$i 51$i++) {
    if (
    $_POST['check'.$i] == $i) {$ifchecked[$i] = 'checked ';}
    else { 
    $ifchecked[$i] = ''; }
    $checkboxes .= '<input type="checkbox" name="check'.$i.'" id="check'.$i.'" checked="'.$ifchecked[$i].'"/>'."\n";
    }


    $stringData =

    '<head>
    </head>

    <form id="form1" name="form1" method="post" action="writeform.php">
    <input type="text" name="1" id="1" value="'
    .$_POST['1'].'"/>
    <br />
    <input type="text" name="2" id="2"/>
    <br />
    '
    .$checkboxes.'
    <br />
    <input type="submit" name="button" id="button" value="Save" />
    </form>

    <body>
    </html>
    '





    ;
    fwrite($file$stringData);
    fclose($file);

    ?> 
    Last edited by tech_support; 07-10-2007 at 07:52 AM. Reason: Mistake...
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You could do a foreach loop using $_POST, and then check if the name is checkbox____.

    PHP Code:
    <?php
    foreach ($_POST as $key => $value) { //run through each $_POST variable, $key as the name, $value as value
    if (strpos($key,'checkbox')===0) { //if the name of that variable starts with checkbox...
    if ($value==1) { //and it is checked
    $$key ' checked'//set $$key, meaning $checkbox#, to checked
    }
    //end ifs/foreach
    }
    //do your code below for the form, using explicit variables
    //like $checkbox34
    ?>
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    PHP Code:
    $$key 
    ?
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    See the comments I added (edited).

    $$key is the variably-named variable, such as $checkbox5, when $key == 'checkbox5'.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jun 2007
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Can't seem to get it to work...see my code below. The regular post contents works, but not the checkbox status.

    PHP Code:
    <?php
    $file 
    fopen("formsaved.html""w") or exit("Unable to open file!");

    foreach (
    $_POST as $key => $value) { //run through each $_POST variable, $key as the name, $value as value
    if (strpos($key,'checkbox')===0) { //if the name of that variable starts with checkbox...
    if ($value==1) { //and it is checked
    $$key 'checked'//set $$key, meaning $checkbox#, to checked
    }
    //end ifs/foreach
    }
    //do your code below for the form, using explicit variables
    //like $checkbox34

    $stringData 

    '<head>
    </head>

    <form id="form1" name="form1" method="post" action="writeform.php">
      <input type="text" name="1" id="1" value="'
    .$_POST['1'].'"/>
      <br />
      <input type="text" name="2" id="2"/>
      <br />
      <input type="checkbox" name="checkbox1" id="checkbox1"  checked="'
    .$$key.'"/>
      <br />
      <input type="submit" name="button" id="button" value="Save" />
    </form>

    <body>
    </html>
    '





    ;
    fwrite($file$stringData);  
    fclose($file);

    ?>

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Not $$key in the form, but the explicit name of the variable, $checkbox1, then $checkbox2, etc.

    $$key will have the value of $[$key], meaning $last_post_variable_checked, so, probably $button, which is not what you want.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Have you tried the one I posted?

    //Me just confused at djr33's one
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

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
  •