简体   繁体   中英

php pass parameter in url

I have a form and a PHP function:

<?php

if(isset($_GET['sub'])) {
  var_dump($_REQUEST);
  die;
}
?>

<form action="index.php?a=12" method="get">
  <input name="sub" type="submit" value="Calc">
</form>

But when I click to submit I get nothing.

How can I pass variable a in URL (without any inputs) and why this method doesn't work?

You need a , but you get sub . It's problem. And you must use a for input . You can see difference in this code.

<?php
if(isset($_GET['a'])) {
var_dump($_REQUEST);die;
}
?>

<form action="index.php?a=12" method="get">
<input name="a" type="submit" value="Calc">
</form>

Output:

array (size=1) 'a' => string 'Calc' (length=4)

This method does not work because you put a not sub variable in URL, so I think you need to change to this :

<?php

if(isset($_GET['a'])) {
    var_dump($_REQUEST);
    die;
}
?>

Try to do a var_dump($_REQUEST); before your if to see what you have.

you have 2 options to send variables in the URL:

  1. use a hyperlink
 <a href="index.php?a=12">Test!</a> 
  1. Use a form with GET, and have the field as hidden.
 <form action="index.php" method="get"> <input name="a" type="hidden" value="12"> <input name="sub" type="submit" value="Calc"> </form>

You are mixing both methods, and the contents of the GET form, override your action.

Try this, instead of:

if(isset($_GET['sub'])) {

use:

if(isset($_GET["sub"))}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM