0

I want to pass a select option value through hyperlink to the next page and for this I have this code:

<!doctype html>
<html lang="en">
<head>
</head>
<body>
<form name="search_form" role="form" method="GET" id="search_form" action="SearchResults.php">
  <?php 
    try {
      $conn = new PDO('sqlite:db/MyDatabase.db');
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $stmt = $conn->prepare("SELECT * FROM attributes WHERE attributename='mushtype'");
      $stmt->execute();
      $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
  ?>

  <select id="mushtype" name="mushtype">
    <option value="*" selected>Mushroom types</option>
    <?php foreach($data as $row): ?>
        <option value="<?php echo $row['idattributevalue']; ?>"><?php echo $row['attributevalueEN']; ?></option>
    <?php endforeach; ?>
  </select>

  <?php 
    } catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
    }
    $conn = null;
  ?>

  <a href="SearchResults.php?mushtype=<?php echo $row['idattributevalue']?>">Find Your Mushroom</a>

</form>
</body>
</html>

For the default selected option I want to use (*) as value because if no option are selected, will stand as SELECT (all) FROM for my SQL query on the second page. So, i tried to construct my hyperlink, but i don't know how to catch the selected option. I tried to adapt from here

    <?php  
        if(isset($_POST['submit'])){  
        if(!empty($_POST['Movies'])) {  
            $selected = $_POST['Movies'];  
            echo 'You have chosen: ' . $selected;  
        } else {  
            echo 'Please select the value.';  
        }  
        }  
    ?>  

but I'm unable to do it. Rigth now, my code inserting a value, but is without any control upon it.

Please help me to catch the selected option and insert it as value in my hyperlink. Thank you.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Do some searches please there tons of answers, questions here https://stackoverflow.com/questions/2407284/how-to-get-multiple-selected-values-of-select-box-in-php – DLK Dec 18 '22 at 17:42
  • Looks like your form is submitting as a GET, so check the values you are using as you are using `$_POST`. – Nigel Ren Dec 18 '22 at 17:50
  • @Nigel Ren, i dont use $_POST, just showed the section which i tried to adapt without success – Szabolcs Horvath Dec 18 '22 at 17:53
  • 1
    You need to submit the form. A hyperlink doesn't submit the form. You should use a button. Please try some basic html forms tutorials so you can understand key information like this. – ADyson Dec 18 '22 at 18:05

1 Answers1

2

When you use <a>, you do not submit the inputs to the next page. Instead of

<a href="SearchResults.php?mushtype=<?php echo $row['idattributevalue']?>">Find Your Mushroom</a>

simply use

<input type="submit" value="Find Your Mushroom">

before closing the form.

Edit: Depending on the <form method= you use ("get" or "post"), you will then either have $_GET['mushtype'] or $_POST['mushtype'] available in the next page. (Thanks to @ADyson!)

In my tests using method="get", the form inputs make their way to $_GET, but my uri parameters were not there. Using method="post" gave me both my uri parameters in $_GET and form details in $_POST, just in case you want to use both.

If you do not like the button style however, you can change it using CSS to look just like a regular hyperlink.

Skip
  • 95
  • 8
  • 1
    They'll need to change the form's method to Post too – ADyson Dec 19 '22 at 08:10
  • @ADyson When you use the submit-button, request method is POST by default. Using an Uri with appended form parameters is GET. – Skip Dec 19 '22 at 11:19
  • @WolfOfEco sorry but that simply isn't true. Demo: https://jsfiddle.net/ybath3u0/ . If you submit a form without a default method, it defaults to GET. It has nothing to do with whether you use a button or not. I've no idea where you got that idea from - you can't find it in any demos or documentation or anything, because it isn't possible. It's true that a button can have a "formmethod" attribute which can override the `method` attribute of the enclosing `
    ` but that's not what you described - it has to be set explicitly, there is no default.
    – ADyson Dec 19 '22 at 13:24
  • Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formmethod – ADyson Dec 19 '22 at 13:26
  • P.S. The same point can be made about an "input type='submit'" as well. Demo: https://jsfiddle.net/fgd5w9uz/ Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/submit#formmethod – ADyson Dec 19 '22 at 13:33
  • @ADyson Maybe we talk about two different things here. The HTTP request method might be GET in both cases, you might be right about that. I'm talking about the way form data becomes available after submission. They are not delivered in get parameters like uri/?mushtype=One, but in the request body. Get-parameters are available in $_GET, while the ones submitted by forms are available in $_POST. It's been a while since I last used forms. I will setup a form and check $_SERVER['REQUEST_METHOD'], although that is not the point of this question. – Skip Dec 19 '22 at 13:44
  • When the form method is GET, all the variables are submitted as URL parameters. GET methods don't even have a body. Did you look at the demo I posted? When you submit the form in the demo, it tries to visit the URL `https://fiddle.jshell.net/ybath3u0/show/SearchResults.php?mushtype=*` (it fails, obviously, but that's not the point. The Network tool shows you it submits the form as a GET, to that URL, and that there is no body payload, only query parameters in the URL.) – ADyson Dec 19 '22 at 13:47
  • `the ones submitted by forms are available in $_POST`... but **only** if "method='POST'" is specified in the form tag, or an equivalent in the button's formmethod attribute. Otherwise it's a GET and the variables go into $_GET in PHP. I agree, it's clearly been a while since you worked on this stuff :-) – ADyson Dec 19 '22 at 13:54
  • Anyway it _is_ relevant to the question, because without changing the "method" of the OP's form, your answer is incomplete. – ADyson Dec 19 '22 at 13:54
  • 1
    You, Sir, are totally right and I am not. Corrected my answer! – Skip Dec 19 '22 at 14:01
  • Thank you all, for all this contribution, all works well with a little (unexpected) exception for me, should i continue it here or open another question regarding the default selected option value. – Szabolcs Horvath Dec 19 '22 at 19:48
  • 1
    As there may be others who run into the same difficulties you do, I'd suggest to place a new question. Or maybe to find out that others already had them and placed similar questions. Unless it's still the same thing. – Skip Dec 19 '22 at 20:18