1

I am trying to submit a form (or a request) to do some tasks. The form doesn't have input field except a submit button.

I know it's kinda strange but I want to submit the form and get the data by using only php.

Is that possible?

My codes

if($_GET['submitImage']){

   //do the stff I want......

}

<form id='formSubmitImage' name='submitImage' method='get' action='cm_ct_generate_preview.html'>
<?php
        echo "<table>";
          echo "<th>ID</th>";
          echo "<th>Type</th>";
          echo "<th>Name</th>";
          echo "<th>Image</th>";
          foreach($tests as $test){
              echo '<tr>';
                  echo '<td>';
                  echo $test['ID'];
                  echo '</td>';

                  echo '<td>';
                  echo $test['Type'];
                  echo '</td>';

                  echo '<td>';
                  echo $test['Name'];
                  echo '</td>';

                  echo '<td>';
                  echo $test['FileName'];
                  echo '</td>';
              echo '</tr>';
          }
          echo "</table>";
          echo "<input type='submit' value='Set Images'></input><br>";
?>
</form>

Are there any alternative way to do this? Thanks a lot!

1
  • IMHO, this smells bad. You should use the <a> tag do command actions in your application and forms. Jun 28, 2013 at 20:20

3 Answers 3

1

Instead of:

echo "<input type='submit' value='Set Images'></input><br>";

Why not:

echo "<button type='submit'>Set Images</button><br>";
2
  • yes but how do i verify if the user submit the form. I tried if($_GET['submitImage']) but it doesn't work.
    – Rouge
    Apr 22, 2013 at 19:03
  • change method attribute to POST, and then, in PHP: if($_SERVER["REQUEST_METHOD"] == "POST"). Apr 22, 2013 at 19:05
1

You can add a GET parameter to the end of your action URL (in your case it would then be something like cm_ct_generate_preview.html?submitImage=1 or you can add a hidden input field in the form and check if that is submitted, for example:

<input type="hidden" name="submitImage" value="1" />

As your form method is already set to GET it should be sent as a GET parameter.

0

If the user needs to activate it, let them just press the submit button. However if you want to do it automatically, you will need to use a little javascript. To send the data with it, you can just specify your inputs like this:

echo '<input type="hidden" name="id" value="'.$test['ID'].'">';

the input fields will not appear but the data will be send like with an input field.

2
  • I don't need the hidden data. I just want to be able to submit the form without sending any input field data.
    – Rouge
    Apr 22, 2013 at 19:03
  • then you will need javascript to do the job.
    – Xandervr
    Apr 22, 2013 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.