开发者

php a href post isset

<?php echo isset($_GET["name"])?$_GET['name']:'ddd'; ?>
<?php   
... 开发者_JS百科
$url = ("http://localhost/web12/directory/list=".$_GET['name']."");
//echo $url;
...
?>
<form action="t.php" method="post" target="_self">
    <a href="t.php?name=aaa">aaa</a>
    <a href="t.php?name=bbb">bbb</a>
    <a href="t.php?name=ccc">ccc</a>
</form>

How do I make it so that if the post is empty(first time open the t.php),let the $url = ("http://localhost/web12/directory/list=ddd"); ?

Now my default $_GET['name'] echoes nothing.$url =("http://localhost/web12/directory/list=") Thanks.


There are several issues here.

Try not to use ternaries if you can get away with it. They're just hard to read, and can be confusing. This is of course my own opinion. Also, I check with strlen() instead of isset, as I have had issues with isset() and arrays.

<?php

if (strlen($_GET["name"]) > 0) {
    $urlname = $_GET['name'];
    echo "GET: $urlname";
} elseif (strlen($_POST["name"]) > 0) {
    $urlname = $_POST['name'];
    echo "POST: $urlname";
} else {
    $urlname = 'ddd';
    echo "NONE: $urlname";
}

?>

You don't need the parentheses around a string declaration.

<?php

$url = "http://localhost/web12/directory/list=".$urlname;
echo $url;

?>

Forms get or post data only by submitting information, where anchor (a) tags send information through get (the query string after the filename). So you're mixing these without getting the effect you want.

GET

Will send information through the query string (like a link):

<form action="t.php" method="get" target="_self">
 <input type="radio" name="name" value="aaa"/> AAA<br/>
 <input type="radio" name="name" value="bbb"/> BBB<br/>
 <input type="radio" name="name" value="ccc"/> CCC
 <input type="submit"/>
</form>

Note the submit button.

POST

Will submit as post information, without affecting the action url:

<form action="t.php" method="post" target="_self">
 <input type="radio" name="name" value="aaa"/> AAA<br/>
 <input type="radio" name="name" value="bbb"/> BBB<br/>
 <input type="radio" name="name" value="ccc"/> CCC
 <input type="submit"/>
</form>

Note the submit button.

ANCHOR GET

Links do not fire the form submit, no matter if they are within a form element or outside of one. They're just not part of the form submit process.

<a href="t.php?name=aaa">aaa</a>
<a href="t.php?name=bbb">bbb</a>
<a href="t.php?name=ccc">ccc</a>


$variable = isset($_POST['name']) ? $_POST['name'] : 'ddd'; 
... 
$url = "http://localhost/web12/directory/list=" . $variable;


The quick and dirty method would be the

<?
$val = $_POST['val'];
if (!$_POST) {
 // No posted data, so do something else
 $val = 'Default val';
}
?>

Also, you form have method="POST", which will be captured by $_POST, not $_GEt


The cleanest way

<?php

$name = filter_input(INPUT_POST, 'name');
if (!$name) $name = 'ddd';

$url = 'http://localhost/web12/directory/list=' . urlencode($name);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜