Results 1 to 6 of 6

Thread: how to change values in the input fields?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    265

    how to change values in the input fields?

    hello!
    how to use php to change values of text field,checkbox,select box?
    Ex: I have this code:
    <select name="day"><option name="1">1</option><option name="2">2</option></select>

    what is the php code to change the current selected item in the select box to make 2 for example?

    thx

  2. #2
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: how to change values in the input fields?

    If you want to do it "live" then use JavaScript.

    For setting it when the page are being build do sth like
    <select name="day">
    <option name="1" <?php echo("selected='selected'"); ?>>1</option>
    <option name="2">2</option>
    </select>
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    265

    Re: how to change values in the input fields?

    that's dosn't solve the problem. The Problem is as following:
    I wrote a register.php page to register new members but the problem is when the user enter incorrect info like email that is already in the database the register page will display again with a message "duplicate email, choose another" but all of the field are cleared, So he/she have to rewrite info from the begining. So I want to copy the correct data to the fields automatically. I succes in doing that with textfield by write <input name="t" value=$_POST['t']> but with the checkbox and listbox I didn't until now
    thx

  4. #4
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: how to change values in the input fields?

    just gonna write "pseudo" code ok?
    Code:
    if (...posted...)  {
     $email = $_POST['email'];
     $this= $_POST['that'];
     $option= $_POST['option'];
     .... check if all values are valid, if not dont call function that do the job, but display error like...:
     $error = check_email($email);
    }
     <div><?php echo($error); ?></div>
     <input type='text' name='email' id='email' value='<?php echo($email);> ' />
     <input type='text' name='email' id='email' value='<?php echo($email);> ' />
     <select name='option' id='option'>
      <option id='eat' name='eat' <?php if($option=="eat") echo("selected='selected'"); ?>EAT</option>
     <option id='drink' name='drink' <?php if($option=="drink") echo("selected='selected'"); ?>DRINK</option>
     </select>
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2005
    Posts
    265

    Re: how to change values in the input fields?

    thx bro it was a good solution for the birthdate listboxes because I using for loop to fill the list of day,month,year with the numers.
    But it is very bad solution for the list of the countries?! I have more than 100 name so it is would be so difficult to add a php statement proper to each of the 100 country?! so there is any way to select the item automatically by java or php?

    2nd Question:
    I used this to recover the state of the checkbox:
    if(!isset($_POST['sharemail'])){
    echo "checked='checked'";
    }else{
    echo ($_POST['sharemail']=="on")?"checked='checked'":"";
    }
    but is don't works why?
    thx

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: how to change values in the input fields?

    PHP Code:
    <select name="country">
      <?php foreach ($countries as $country): ?>
        <?php if ($country == $selected_country): ?>
          <option value="<?php echo $country?>" selected="selected"><?php echo $country?></option>
        <?php else: ?>
          <option value="<?php echo $country?>"><?php echo $country?></option>
        <?php endif; ?>
      <?php endforeach; ?>
    </select>

Posting Permissions

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



Click Here to Expand Forum to Full Width