Setting the value of a input field using a variable

Discussion in 'PHP' started by locus, Feb 27, 2007.

  1. #1
    Is it possible to do something like this?:

    <input type="text" maxlength="10" length="15"  value= some_string_returning_function($value)  />
    
    PHP:

    I am trying to send a form back to the user for him/her to correct a field,
    but i want the other fields (already validated) to still have what he had previously entered in them.

    Please feel free to be generous with your suggestions, your time is greatly appreciated.
     
    locus, Feb 27, 2007 IP
  2. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #2
    Just use sessions.
    Put start_session();

    at the top of your page, then in the values of your input fields enter something like:
    <input type="text" name="somename" maxlength="10" length="15"  value="<?php if (isset($_SESSION['somename'])) {echo $_SESSION['somename'];} ?>"  />
    Code (markup):
    Then when this form is subbmitted make $_SESSION['somename'] = $_POST['somename']. If that field was entered incorrectly send them back to that page, and the value that was entered previously will be displayed.
     
    papa_face, Feb 27, 2007 IP
  3. Francky683

    Francky683 Active Member

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #3
    If it sends the user to a total different page, then you could do have all the $_POST variables to be added as hidden values, then send the user back and use the $_POST variables, here's an example of whta your code could be like :

    First form [form.php] :

    
    <?php
     $fieldsValue['your_value'] = isset($_POST['your_value']) ? $_POST : null;
    ?>
    <form action="submit.php" method="post">
     <input type="text" name="your_input" value="<?php echo $fieldsValue['your_input']; ?>"/>
     <input type="submit" value="Submit" />
    </form>
    Code (markup):
    And then [submit.php] :

    
    <?php
    if($_POST['your_input'] !== 'My value'){
     echo '<form action="form.php" method="post" name="my_form">';
     foreach($_POST as $inputName => $inputValue){
      echo '<input type="hidden" value="'.$inputValue.'" name="'.$inputName.'" />';
     }
     echo '
      The value isn't good. You will be brought back.
      <script language="javascript" type="text/javascript">setTimeout("document.my_form.submit","3000");</script>
      </form>
     ';
    ?>
    Code (markup):
    It sure would need to be adapted to your script, and there might be mistakes up there, but that's how I would be doing it in your case.
     
    Francky683, Feb 27, 2007 IP