0

I am trying to call a function via a submit button, but I am unable to submit it.

<!DOCTYPE html>
<html>
<body>

<h2>API Call</h2>

<form method="post" action="display()">
  <label for="gid">Global Device ID:</label><br>
  <input type="text" id="gid" name="gid" value="m99002021" readonly><br>
  <label for="type">Type:</label><br>
  <input type="text" id="type" name="type" value="EVNT" readonly><br><br>
  <label for="start">Start Date Time:</label><br>
  <input type="text" id="start" name="start" value="2020-09-01 00:00:00" readonly><br><br>
  <label for="end">End Date Time:</label><br>
  <input type="text" id="end" name="end" value="2020-09-30 23:59:59" readonly><br><br>
  <input type="submit" value="Execute">
</form> 

<?php
function display()
{
  echo "hello".$_POST["gid"]."<br>";
  echo "hello".$_POST["type"]."<br>";
  echo "hello".$_POST["start"]."<br>";
  echo "hello".$_POST["end"]."<br>";
}
if($_SERVER['REQUEST_METHOD']=='POST')
{
       display();
} 

?>

</body>
</html>

When clicking on the execute button I am getting bellow page

enter image description here

The URL is file:///C:/Users/Faisal/Desktop/display()

Update 1

I have used xampp for my webserver. Now I am trying to access it via http://localhost/udil/backend/main.php and it gives me bellow page

enter image description here

when I click on the execute button I am getting below the page

enter image description here

How can I properly submit it?

Any help would be highly appreciated.

Moeez
  • 494
  • 9
  • 55
  • 147

3 Answers3

2

Since you are now using XAMPP to use PHP, we can start working on your misconception.

In your code you have this line
<form method="post" action="display()">

It seems as if you were thinking that the action is the JavaScript action to be called. But that is not the case.

The action of a <form> does not call JavaScript, but it sends the form with all the form data to the URL given in this attribute. Since there is the value of display() in your action attribute, the Browser tries to send the data to the address display() relative to your form URL. But this is an address the webserver has no answer to and thus sends back an 404 error.

The question for you that I have now is: what should happen after the form was sent? Should the form data just be written to the document? I assume that is what you want to achieve. If yes, try this code.

<!DOCTYPE html>
<html>
<body>

<h2>API Call</h2>

<?php // Omit the action of the form. 
      // Now your PHP script will be called again, when the form is submitted
?>
<form method="post">
  <label for="gid">Global Device ID:</label><br>
  <input type="text" id="gid" name="gid" value="m99002021" readonly><br>
  <label for="type">Type:</label><br>
  <input type="text" id="type" name="type" value="EVNT" readonly><br><br>
  <label for="start">Start Date Time:</label><br>
  <input type="text" id="start" name="start" value="2020-09-01 00:00:00" readonly><br><br>
  <label for="end">End Date Time:</label><br>
  <input type="text" id="end" name="end" value="2020-09-30 23:59:59" readonly><br><br>
  <input type="submit" value="Execute">
</form> 

<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
  echo "hello".$_POST["gid"]."<br>";
  echo "hello".$_POST["type"]."<br>";
  echo "hello".$_POST["start"]."<br>";
  echo "hello".$_POST["end"]."<br>";
} 

?>

</body>
</html>

No JavaScript is involved in this code, because you don't need it.

BTW. This code should be in a PHP file, not an HTML file

yunzen
  • 32,854
  • 11
  • 73
  • 106
0

Just remove the display() from form and click the Execute button. It will properly work

0

Try like this

<!DOCTYPE html>
<html>
<body>

<h2>API Call</h2>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <label for="gid">Global Device ID:</label><br>
  <input type="text" id="gid" name="gid" value="m99002021" readonly><br>
  <label for="type">Type:</label><br>
  <input type="text" id="type" name="type" value="EVNT" readonly><br><br>
  <label for="start">Start Date Time:</label><br>
  <input type="text" id="start" name="start" value="2020-09-01 00:00:00" readonly><br><br>
  <label for="end">End Date Time:</label><br>
  <input type="text" id="end" name="end" value="2020-09-30 23:59:59" readonly><br><br>
  <input type="submit" value="Execute">
</form> 

<?php
function display()
{
  if(isset($_POST['submit'])
  {
    echo "hello".$_POST["gid"]."<br>";
    echo "hello".$_POST["type"]."<br>";
    echo "hello".$_POST["start"]."<br>";
    echo "hello".$_POST["end"]."<br>";
  }
}
if($_SERVER['REQUEST_METHOD']=='POST')
{
       display();
} 

?>

</body>
</html>
Sohail Ansari
  • 350
  • 4
  • 10