tags:

views:

430

answers:

7

Hello,

I tried to redirect to the current page with form in my php application. Now i have met 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 the current page, because i use the single entry in my php application.

With the code upon, When i click the button, it redirects to the homepage.

And I tried to use:

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

but it still doesn't work.

So i have to ask for help from you. Do you have any ideas about that? How to fix it? Thanks in advance.

+1  A: 

leave the action part empty to redirect to current page eg:

<form action="">

or if you want to redirect to some other page, you can do so like this:

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

If you want to append query string then:

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

If you want to redirect to a page one directory back then:

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

Two directories:

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

inside a nother folder

<form action="yourfolder/somepage.php?var=value&var2=value">
Sarfraz
@Thanks Sarfraz Ahmed, I've used the two ways you recommended. It doesn't work because when i use "somepage.php", i can find the page. I have used the single index page to control the access to all the other pages.
garcon1986
then you can specify that like this too action="index.php?page=yourpage"
Sarfraz
I have changed it. I have no idea why it doesn't work. Frustrated.
garcon1986
A: 

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

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

I recommend to build a link helper that manages your routes.

erenon
@Thanks ereno, with the code: <form name="myform" action="<?php echo $_SERVER['PHP_SELF'].'?page=matching'; ?>" method="GET">. It redirects to the index.php. I think you're right, but i don't know why it's wrong.
garcon1986
A: 
<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>

Alsoy, the JS function func_load needs not to redirect the page to somewhere.

Benoit Vidis
@BenoKrapo, Thanks, i think the hidden input is not working in this case.
garcon1986
A: 

PHP_SELF doesnt include Query string, you have to tag that on the end

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

then set that as your form action:

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

Thou i guess just doing:

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

Might work too, not sure what it will do if the QUERY_STRING is empty thou

Andy
@thanks, i've tried this way. <form name="myform" action="<?php echo $_SERVER['PHP_SELF'].'?page=matching'; ?>" method="GET">. Why is it wrong?
garcon1986
A: 

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" ...
turbod
garcon1986
this work only post method, dont work the get method.<code><?phpswitch ($_REQUEST['site']){ case 'addcart': print $_POST['first']; break; default: break;}?><form action="<?php echo $_SERVER['PHP_SELF'];?>?site=addcart" name="myform" method="post" > <input name="first" type="text" /> <input type="submit" value="ok" name="akcio" /></form></code>
turbod
A: 

Work only with post method.

turbod
A: 

Hi, I have following situation :

I Have form say form1, through form1 i input some data. this data will go to form2 in post method. now is it possible that i redirect the user to different form other than form2 say form3.

here condition is that i don't have access to form2 source code.

Please suggest what should i do in this case ?

Thanks,

Girish Meena