How to redirect to current page with form in php?

I tried redirecting to the current page with the form in my php application. Now I am facing a problem.

<form name="myform" action="?page=matching" method="GET">

<input id="match_button" type="submit" name="button" value="button" onClick="func_load3()" />

</form>

action="?page=matching" means current page, because I use a single entry in my php application.

With the code on, When I click the button, it redirects to the home page.

And I tried to use:

<form name="myform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">

but it still doesn't work.

Therefore, I must ask you for help. Do you have any ideas on this? How to fix it? Thank you in advance.

+3
source share
6 answers

leave part of the action blank to redirect to the current page, for example:

<form action="">

, :

<form action="somepage.php">  // page must be in the same directory

, :

<form action="somepage.php?var=value&var2=value">

, :

<form action="../somepage.php?var=value&var2=value">

:

<form action="../../somepage.php?var=value&var2=value"> and so on

<form action="yourfolder/somepage.php?var=value&var2=value">
+13

PHP_SELF ,

<?php
$formAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) { 
    $formAction .= "?" . $_SERVER['QUERY_STRING']; 
}
?>

:

<form name="myform" action="<?=$formaction;?>" method="GET">

, :

<form name="myform" action="<?=$_SERVER['PHP_SELF']. "?" . $_SERVER['QUERY_STRING'];?>" method="GET">

, , , QUERY_STRING .

+2
<form name="myform" method="GET">
  <input type="hidden" name="page" value="matching" />
  <input id="match_button" type="submit" name="button" value="button" onClick="func_load3()" />
</form>

In addition, the JS func_load function should not redirect the page anywhere.

+1
source

$_SERVER[PHP_SELF]probably index.php. Have you looked at him? You need:

<?php echo $_SERVER['PHP_SELF']. '?page=matching'; ?>

I recommend creating a link helper that manages your routes.

0
source

try this index.php:

switch ($_REQUEST['site']){
  case 'somethings':      
     if ($_POST['k_send']){ 
       //somethings
     }
  ...

form.php

<form name="send" action="index.php?site=somethings" 
  <input ...
  <input type="submit" name="k_send" ...
0
source

Work only with the post method.

0
source

Source: https://habr.com/ru/post/1730392/


All Articles