Dor Eliyahu
Dor Eliyahu

Reputation: 37

get the form values in php in the same page of the form

i want to take the < select > value to the php code (above) when i press the submit button

I tried to send the form values to the same page (with action) but it didnt work. what should I do?

<form name="open_file">
      <select name="file" style="width:40%; font-size:20px;">
          <option value="A1" selected="selected" >A1</option>
          <option value="A2" >A2</option>
          <option value="A3" >A3</option>
      </select>
      <button type="submit" name="submit">UPLOAD</button>
</form>
<?php
    //the <select> value should be in $file.
    $file= ...?
?>

thank you !

Upvotes: 1

Views: 6629

Answers (2)

user6360214
user6360214

Reputation:

Without defining the action attribute, form element WILL NOT send the request to any page via any method.

In addition, using button, probably would the server not send the request. Use input element instead.

If you really wish to send the data to the same page, use:

<form name="open_file" action="" method="GET">
<!--your link to this page in action attribute-->
  <select name="file" style="width:40%; font-size:20px;">
      <option value="A1" selected="selected" >A1</option>
      <option value="A2" >A2</option>
      <option value="A3" >A3</option>
  </select>
  <input type="submit" name="submit" value="UPLOAD" />
</form>

Plus, it is not a good practice to process form data after outputting HTML. Put the PHP code at the beginning of the document.

Upvotes: 1

Sanchit Gupta
Sanchit Gupta

Reputation: 3224

First, you have to define your form action and method like :

<form name="open_file" action="" method='GET'>
      <select name="file" style="width:40%; font-size:20px;">
          <option value="A1" selected="selected" >A1</option>
          <option value="A2" >A2</option>
          <option value="A3" >A3</option>
      </select>
      <button type="submit" name="submit">UPLOAD</button>
</form>
<!-- GET YOUR SELECT VALUES USING PHP -->

<?php 
    if(isset($_GET['file'])){
        $file = $_GET['file'];
        // Now you can use $file variable according to your code requirements
    }
?>

For more detail : PHP Form Handling

Upvotes: 0

Related Questions