-2

I have a simple form with a select tag, I would like that after clicking on submit, what was marked was remembered.

Could you help me?? How to correct the code below? I'm sure that I can't find it correctly on the Internet so I'm asking you for help.

<form action="select.php" method="post">
    <select name="select">
        <option value="test1">test1</option>
        <option value="test2">test2</option>
        <option value="test3">test3</option>
        <option value="test4">test4</option>
    </select>
    <input type="submit" name="submit" value="Send">
</form>
makoszet
  • 19
  • 6
  • You can make an Ajax call to prevent page load. See this [Form submit with AJAX passing form data to PHP without page refresh](https://stackoverflow.com/questions/16616250/form-submit-with-ajax-passing-form-data-to-php-without-page-refresh?answertab=votes#tab-top) – Nimitt Shah Nov 05 '18 at 14:45

1 Answers1

1

Even though your question is not clear enough, as your question is not that straight forward.

So I am going to give an answer based on an assumption :).

  1. I am assuming you want to achieve this with php only.
  2. I am assuming that you are using POST request

  3. I am assuming that your form action='select.php' is the name of the page where your above code reside.

Submitting the form to the same page.

<!DOCTYPE html>
<html>
    <head>
        <title>This is the select page</title>
    </head>
<body>
    <?php 

        $select = '';

        if( isset($_POST['select']) ){
            $select = $_POST['select'];
        }

    ?>

    <form action="select.php" method="post">
        <select name="select">
            <option value="test1" <?php if($select == 'test1'): ?> selected <?php endif; ?>>test1</option>
            <option value="test2" <?php if($select == 'test2'): ?> selected <?php endif; ?>>test2</option>
            <option value="test3" <?php if($select == 'test3'): ?> selected <?php endif; ?>>test3</option>
            <option value="test4" <?php if($select == 'test4'): ?> selected <?php endif; ?>>test4</option>
    </select>
            <input type="submit" name="submit" value="Send">
    </form>
</body>
</html>

Submitting the form to different page.

<!DOCTYPE html>
<html>
    <head>
        <title>This is the select page</title>
    </head>
<body>
    <form action="another-page.php" method="post">
        <select name="select">
            <option value="test1">test1</option>
            <option value="test2">test2</option>
            <option value="test3">test3</option>
            <option value="test4">test4</option>
    </select>
            <input type="submit" name="submit" value="Send">
    </form>
</body>
</html>

and in the another-page.php you will have it written like this

<!DOCTYPE html>
<html>
    <head>
        <title>This is the another page</title>
    </head>
<body>
    <form>
        <select name="select2">
            <?php 
                $select = $_POST['select'];
                if( isset($select) ){
                    echo "<option value='{$select}'> {$select} </option>";
                }else{
                    echo "<option value='' selected disabled> Nothing was selected </option>";
                }
            ?>
        </select>
    </form>
</body>
</html>
Solar
  • 870
  • 9
  • 18
  • I am sorry that I wrote so not clearly, I hope my next posts will be more clear. Thank you for your comprehensive answer. You are the best! :) – makoszet Nov 06 '18 at 08:16
  • You're welcome. Glad I could help; If it suites your need please mark it as the accepted answer. – Solar Nov 06 '18 at 09:20