Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Save form script. Problem

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

    Default

    My code is easy. It loops through, uses the $_POST[VALUEHERE] as a variable, then saves that as ' checked', if the checkbox was in the form.

    But either way, the code should work if you just use this line:
    <input type="checkbox" name="checkbox1" id="checkbox1" checked="'.$checkbox1.'"/>
    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

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

    Default

    Ok status update.

    Tried your script tech_support and it generates 50 checkboxes in a row. I was probably a bit unclear, I have checkboxes all around my form and the status of these needs to be written to the formsaved.html. Right now I am fiddling with the solution from djr33. The result written to the saved form regardless of if checkbox1 is checked or not is

    checked=""

    It seems checked="checked" is never written and that checked="" is the same as checked in html code?? Something wrong at least.

    Thanks for your help guys. Php is really fun but a bit frustrating sometimes


    My current code is

    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="'
    .$checkbox1.'"/>
      <br />
      <input type="submit" name="button" id="button" value="Save" />
    </form>

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

    ?>

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

    Default

    Trying a new approach on my own but there is something wrong in the code. Anyone help me?

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

    function 
    checkbox($checkbox)
        {
        (if 
    $_POST[$checkbox.]==1) echo 'checked="checked"';
        }

    //beräkningar
    $sumformat number_format(round($_POST['1'] + $_POST['2']), 0','' ');

    //data som skrivs till htmlfil
    $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" value="'
    .$_POST['2'].'"/>
      <br />
      <input type="text" name="sum" id="sum" value="'
    .$sumformat.'"/>
      <br />
    <input type="checkbox" name="checkbox1" id="checkbox1"'
    checkbox(checkbox1);'/>
      <br />
      <input type="submit" name="button" id="button" value="Save" />
    </form>

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





    header("Location: formblank.html");


    ?>

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

    Default

    PHP Code:
    function checkbox($checkbox)
        {
        (if 
    $_POST[$checkbox.]==1) echo 'checked="checked"';
        } 
    No need for the dot after $checkbox
    PHP Code:
    "'checkbox(checkbox1);'/> 
    checkbox1 should be just 1
    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. #15
    Join Date
    Jun 2007
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    But it needs to be checkbox1 because the checkbox element is called this?

    There is something bad in my code since it doesn't execute the code at the bottom (forward to page). Any clues?

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

    function checkbox($checkbox)
        {
        (if 
    $_POST[$checkbox]==1) echo 'checked="checked"';
        }

    //beräkningar
    $sumformat number_format(round($_POST['1'] + $_POST['2']), 0','' ');

    //data som skrivs till htmlfil
    $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" value="'
    .$_POST['2'].'"/>
      <br />
      <input type="text" name="sum" id="sum" value="'
    .$sumformat.'"/>
      <br />
    <input type="checkbox" name="checkbox1" id="checkbox1"'
    checkbox(checkbox1);'/>
      <br />
      <input type="submit" name="button" id="button" value="Save" />
    </form>

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





    header("Location: formblank.html");


    ?>

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

    Default

    if ($value==1) { //and it is checked

    try changing that to
    if ($value=='checked') { //and it is checked

    Since you are using 'checked', rather than '1', as I am used to, with checkboxes.
    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

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

    Default

    Alright, now I have got a working solution (see code below). Apparently the value of a checkbox when checked is 'on'. I also changed the code since checked="" seems to be the same as checked="checked".

    Thanks for all help!

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

    //script för att hantera checkboxes
    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=='on') { //and it is checked
    $$key 'checked="checked"'//set $$key, meaning $checkbox#, to checked
    }
    //end ifs/foreach
    }
    //do your code below for the form, using explicit variables
    //like $checkbox34

    //beräkningar
    $sumformat number_format(round($_POST['1'] + $_POST['2']), 0','' ');

    //data som skrivs till htmlfil
    $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" value="'
    .$_POST['2'].'"/>
      <br />
      <input type="text" name="sum" id="sum" value="'
    .$sumformat.'"/>
      <br />
    <input type="checkbox" name="checkbox1" id="checkbox1"'
    .$checkbox1.'/>
      <input name="checkbox2" type="checkbox" id="checkbox2" '
    .$checkbox2.'/>

      <br />
      <input type="submit" name="button" id="button" value="Save" />
    </form>

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





    header("Location: formblank.html");


    ?>

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
  •