Solution 1 :

When you inject PHP code to display certain value into your HTML, you have to echo the value itself, so:

Instead of:

<input class="param" name="OriginDepartureDate" type="text" value="<?php $value ?>">

you need:

<input class="param" name="OriginDepartureDate" type="text" value="<?php echo $value ?>">

Solution 2 :

I believe you forgot to set the option value. You can read more about it here Using $_POST to get select option value from HTML

// options need to have a value
<option value="<?=$orderby?>"><?= $orderby ?></option>

Solution 3 :

Here: If isset $_POST

and here: Avoid using isset …

You can read more. Simply it is more safe to test the content of the variable.
I can recommend to use

 <input type="submit" name="sent" value="send/OK" />

This gives you expected behaviour with isset().

Problem :

I have written an html page with a form; <form action="" method="post">. As you can see this form should post to the same url on which the form is placed. I am using PHP to capture the post;

if(isset($_POST['submit'])) {
    // do stuff

In the form there is a submit button <button type="submit" name="submit">Send</button> with the correct name attribute. If I log the post variable I get all the named fields from the form but what I do not get is the correct values of those fields.

I’ll add a slightly abbreviated version of the form:

<form action="" method="post">
  <table class="table my-5" id="table">
    <?php foreach($parameters as $key => $value): ?>
          <h6><?= $key ?></h6>
            case 'Origin':
              echo '<select class="param" name="Origin">';
              foreach($ports as $port){
                echo '<option>' . $port . '</option>';
              echo '</select>';
            case 'OriginDepartureDate':
              echo '<input class="param" name="OriginDepartureDate" type="text" value="' . $value . '">';
            case 'Limit':
              echo '<input class="param" data-key="Limit" name="Limit" type="text" placeholder="' . $value . '">';
              echo '<span class="param">'. $value . '</span>';
          } ?>
    <?php endforeach ?>
  <button type="submit" name="submit">Send</button>

And the parameters being used to build the form:

$parameters['Origin'] = null;
$parameters['OriginDepartureDate'] = null;

$orderbyArr = ['DRS','GRQ','LGG',...];

The weird thing is some values do come through but others don’t, the Origin value comes through and the Limit value also, but the (not “DestinationDepartureDate” but:) OriginDepartureDate does not.

I’ve been looking at my code for a few days now but I can’t find anything obviously wrong in the logic. If some fresh eyes can see something I do not then that would be wonderful.


Comment posted by Professor Abronsius

"I'll add a slightly abbreviated version of the form"

Comment posted by mitkosoft

where/how do you define

Comment posted by Sl4rtib4rtf4st

@CarlBinalla sorry I meant “OriginDepartureDate”, will amend the question.

Comment posted by Sl4rtib4rtf4st

@mitkosoft the $orderbyArr variable is an ordered version of the $ports variable, will amend the question, thanks…

Comment posted by Professor Abronsius

good luck with the debugging!

Comment posted by Sl4rtib4rtf4st

Good catch, I was meaning to use short hand for the echo:

Comment posted by Professor Abronsius

Note my downvote btw but if there is no value set on an

Comment posted by alithedeveloper

It is always best practice to explicitly give value to your option.

Comment posted by Professor Abronsius

I tend to agree

Comment posted by Sl4rtib4rtf4st

@alithedeveloper, it’s a good point even though it is not the cause for my problem. Adding a value to the option is little effort even though the gains are similarly little…

Comment posted by Sl4rtib4rtf4st

Normally I test against a