我可以使用PHP print作为输入字段的默认值吗?

时间:2013-01-09 09:02:36

标签: php

我已经放置了一个用于获取当前页面URL的PHP​​代码,现在我可以通过编写此代码来获取当前页面URL

<?php print(selfURL()); ?>

现在,我想知道如何将此代码编写为输入字段的默认值? 实际上我希望输入字段自动填充当前页面URL。

当我使用<?php print(selfURL()); ?>作为value="<?php print(selfURL()); ?>"时,简单显示文字并且不显示当前网址。但是当我将它粘贴到其他地方时,它的工作正常。

有什么建议吗?

更新

伙计们,这是该页面的完整代码,可能会有所帮助!

<?php
function selfURL() { $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""; $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; } function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }
?>
<?php 
// Start YOURLS engine
require_once( dirname(__FILE__).'/includes/load-yourls.php' );
?>
<!DOCTYPE html>
<html>
<head>
<title>YOURLS Public Interface Sample</title>
<style>
body {
    background:#E3F3FF;
    color:#595441;
    font:16px/30px verdana,arial,sans-serif;
}
a, a:visited {color:#2A85B3}
h1 {text-align:center; color:#2A85B3}
h2 {
    border-bottom:1px solid #2A85B3;
    color:#2A85B3;
    margin-left:-10px;
    padding-left:25px;
    width:80%;
}
#container {
    width: 780px;
    margin-left: auto;
    margin-right: auto; 
    background-color: #fff;
    border: 3px solid #2A85B3;
    padding: 10px;
    margin-top: -13px;
    -moz-border-radius:15px;
    -webkit-border-radius:15px;
}
#footer {
    text-align:center;
    margin-top:20px;
}
#footer p {
    padding:5px;
    background:white;
    margin:0 auto;
    width:750px;
    -moz-border-radius:10px;
    border-radius:10px;
    -webkit-border-radius:10px;
    border:1px solid #2A85B3;
    -moz-border-radius-bottomleft:35px;
    -moz-border-radius-bottomright:35px;
    -webkit-border-bottom-left-radius:25px;
    -webkit-border-bottom-right-radius:25px;
}
#footer p a {
    background:#fff url(http://yourls.org/images/favicon.gif) 2px center no-repeat;
    padding-left:20px;
}
div#copybox { width:600px; height:auto;}
div#sharebox {height:auto; width:600px; margin-top:20px;}
</style>
<link rel="stylesheet" href="<?php echo YOURLS_SITE; ?>/css/share.css?v=<?php echo YOURLS_VERSION; ?>" type="text/css" media="screen" />
<script src="<?php echo YOURLS_SITE; ?>/js/jquery-1.6.1.min.js" type="text/javascript"></script>
<script src="<?php echo YOURLS_SITE; ?>/js/ZeroClipboard.js?v=<?php echo YOURLS_VERSION; ?>" type="text/javascript"></script>
<script type="text/javascript">ZeroClipboard.setMoviePath( '<?php echo YOURLS_SITE; ?>/js/ZeroClipboard.swf' );</script>
<script src="<?php echo YOURLS_SITE; ?>/js/share.js?v=<?php echo YOURLS_VERSION; ?>" type="text/javascript"></script>
</head>

<body>
<h1>YOURLS: Your Own URL Shortener</h1>

<div id="container">

    <?php

    // Part to be executed if FORM has been submitted
    if ( isset($_REQUEST['url']) ) {

        $url     = yourls_sanitize_url( $_REQUEST['url'] );

        $return  = yourls_add_new_link( $url );

        $shorturl = isset( $return['shorturl'] ) ? $return['shorturl'] : '';


        echo <<<RESULT
        <h2>URL has been shortened</h2>
        <p>Original URL: <code><a href="$url">$url</a></code></p>
        <p>Short URL: <code><a href="$shorturl">$shorturl</a></code></p>

RESULT;



    // Part to be executed when no form has been submitted
    } else {

        $site = YOURLS_SITE;

        echo <<<HTML
        <h2>Enter a new URL to shorten</h2>
        <form method="post" action="">
        <p><label>URL: <input type="text" name="url" value="<?php echo selfURL(); ?>" size="70" /></label></p>
        <p><label>Optional custom keyword: $site/<input type="text" name="keyword" size="8" /></label></p>
        <p><label>Optional title: <input type="text" name="title" size="57" /></label></p>
        <p><input type="submit" value="Shorten" /></p>
        </form> 
HTML;

    }

    ?>


</div>





<?php print(selfURL()); ?>

</body>
</html>

3 个答案:

答案 0 :(得分:2)

试试这个,

<input name="textVal" type="text" val=<?php echo selfURL(); ?> />

答案 1 :(得分:1)

您可以使用以下内容。

方法1

 <input name="myinput" type = "text" value = "<?php print(selfURL()); ?>" />

方法2

<input name="myinput" type = "text" value = "<?php echo selfURL(); ?>" />

问题更新后的答案

<input type="text" name="" id="" value="<?php echo $_SERVER['PHP_SELF']; ?>"/>

答案 2 :(得分:0)

以下是解决方案:

$_SERVER['PHP_SELF'];
经测试的代码如下

<input type="text" name="" id="" value="<?php echo $_SERVER['PHP_SELF']; ?>"/>

您的代码编辑

$var = $_SERVER['PHP_SELF'];
OR
$var = selfURL();

<p><label>URL: <input type="text" name="url" value="$var" size="70" /></label></p>

您在her-doc语法中添加了php,这就是错误