0

i have a php file which stores a variable, and i need to create a submit form which passes this variable.

So the code of the current file is:

if(isset($_POST['Submit'])) {
    echo "<pre>";
    $checked = implode(',', $_POST['checkbox']);
    echo $checked;
}

I have to insert the submit form and on click it goes to another php file, i need to get there the $checked variable.. how can i store this variable?..

Thanks you!

1 Answer 1

2

The principle of POSTing is that you can send all the data to the next .php page.

<?php
    if ($checked == 1) {
        $checked = 'checked="checked"';
    }
    else {
        $checked = '';
    }
?>

<form action="POST" method="target_file.php">
    <input type="hidden" name="variableA" value="Something I want target_file.php to know" />
    <input type="hidden" name="variableB" value="Something else I want target_file.php to know" />
    <input type="checkbox" name="gender" value="male" <?php echo $checked ?>" />
</form>

Your target_file.php:

echo "Here I am :), variableA: ".$_POST['variableA'];
echo "Here I am :), variableB: ".$_POST['variableB'];
echo "Here I am :), My gender is: ".$_POST['gender'];

Also, don't trust on checking your field values that are send through the $_POST. Check what every value is and if it meets certain criteria before sub-statements may be executed.

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

6 Comments

Thanks, so, if i just go and put instead of Variable a the $checked, it would be all ok? I'll test this, thanks :)
Every field within in the form tag will be parsed. But only when it has a "name" attribute. If "value" is not set while a "name" attribute 'is' set, it just passed an empty string ''. So if you have these input fields: name, lastname, phone, age You will get this $_POST: Array( name => 'Value', lastname => 'Value', phone => 'Value', age => 'Value' )
Allendar, thanks, what i need is to give the value a variable... like value = $checked and then print the value.. can i do that?
Yes. You can trigger a inline PHP tag: **#input type="checkbox" name="checked" checked="#?php echo $checked; ?#" /# (Replace the # with > and <
Allendar, thanks, please in order not to disturb you (you helped me enough, can you please update your answer to me... to see how it works, because i don't know how to get the variable then..thanks..
|

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.