-1

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): ?>
      <tr>
        <td>
          <h6><?= $key ?></h6>
        </td>
        <td>
        <?php 
          switch($key){
            case 'Origin':
              echo '<select class="param" name="Origin">';
              foreach($ports as $port){
                echo '<option>' . $port . '</option>';
              }
              echo '</select>';
              break;
            case 'OriginDepartureDate':
              echo '<input class="param" name="OriginDepartureDate" type="text" value="' . $value . '">';
              break;
            case 'Limit':
              echo '<input class="param" data-key="Limit" name="Limit" type="text" placeholder="' . $value . '">';
              break;
            default:
              echo '<span class="param">'. $value . '</span>';
              break;
          } ?>
        </td>
      </tr>
    <?php endforeach ?>
  </table>
  <button type="submit" name="submit">Send</button>
</form>

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.

AndrewRMillar
  • 608
  • 1
  • 7
  • 17

3 Answers3

2

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 ?>">
mitkosoft
  • 5,262
  • 1
  • 13
  • 31
  • Good catch, I was meaning to use short hand for the echo: `= ... ?>`, but apparently forgot. The only reason I don't flag this as the correct answers is because the value always gets changed before submitting the form. And still the value didn't come through. – AndrewRMillar Jan 30 '20 at 10:30
0

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>
alithedeveloper
  • 687
  • 5
  • 17
0

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().

schweik
  • 164
  • 7
  • Normally I test against a `!empty()` end then test the contents of the post variable but as this was not critical code or user facing I could not be arsed... – AndrewRMillar Jan 30 '20 at 10:26