1

I am trying to save fields data after submited, Becouse after all the fields are good to go but lets say at the server side the user name is already taken so the form return empty and i dont want that there is the option to do it with PHP like that:

<input value="<?php if(isset($userName)) echo $userName; ?>" />

But the problem is with the radio input, If can some one think about solution about the radio with PHP i will be very thankful, Also i was thinking about Javascript so i will have cleaned code and i was thinking about taking the values from the URL but i am using POST for security reasons.

Summary: If anyone have a solution with PHP or Javascript i will be very thankful, Thank you all and have a nice day.

3
  • Guys thank you for your answers but i dont understand how can they work, i will be happy if you upload your code so i can see how does it work, and thanks again. Commented Jul 29, 2012 at 13:56
  • Your form's action attribute, is it posting back to the same page or another page? Commented Jul 29, 2012 at 14:15
  • All Data is Post at the same page. Commented Jul 29, 2012 at 14:18

3 Answers 3

1

Try this

<form name="myform" action="" method="post">
<input type="radio" name="language" value="Java" <?php echo(@$_POST['language'] == 'Java'?"checked":""); ?> /> Java
<input type="radio" name="language" value="VB.Net" <?php echo(@$_POST['language'] == 'VB.Net'?"checked":""); ?>  /> VB.Net
<input type="radio" name="language" value="PHP" <?php echo(@$_POST['language'] == 'PHP'?"checked":""); ?>  /> PHP

<input type="submit" />

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you codingbiz, its working, can you just tell me why the @ before the post?
@uBlankText It's an error suppression operator, if $_POST['language'] is not defined it won't display an error. See this : php.net/manual/en/language.operators.errorcontrol.php
Thank you HoLyVieR, so its like @echo off, if i understand it right, the error is check for undefined index becouse the language index is not posted yet, am i right?
@uBlankText Yes, when you will load that page the first time there won't be any POST data available so it won't be able to find the index.
I think i found a sort way please take a look: <?php if(isset($_POST['language'])) echo "checked"; ?> less code.
|
1

I think this may help you.

<input type="radio" value="choice1" name="radio_name" <?php echo(@$_POST['radio_name'] == 'on'?"checked":""); ?> />

Comments

1

If you want to automatically select a radio input you can add the attribute checked to it. What you are going to need will look like this :

<form method="POST">
    <?php

        // You have some short of list of possible value //
        $arrRadioValues = array("value1", "value2", "value3");

        // You display them //
        for ($i=0; $i<count($arrRadioValues); $i++) {
            ?>
                <input 
                    type="radio" 
                    name="radioInputName" 
                    value="<?php echo $arrRadioValues[$i]; ?>" 
                    <!-- If the value that was posted is the current one we have to add the "checked" so that it gets selected -->
                    <?php if (isset($_POST['radioInputName']) && $_POST['radioInputName'] == $arrRadioValues[$i]) { echo " checked"; } ?> />
            <?php
        }

    ?>

    <input type="submit" />
</form>

Adding the checked attribute works a little bit in the same as setting a value to an input. It's just that instead of defining the value attributes, you define the checked attribute when you want that radio to be selected.

2 Comments

Thank you HoLyVieR for you anwser, but i dont understand the for loop can you upload your code to an online engine so i can take a look how does it work becouse i dont know how to implement this at my code, and thanks again.
@uBlankText Usually you have some short of list of values for your radio button and you loop in those values to display all the radio control. When you pass the value that has the same value as the one that was posted you just need to add the checked attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.