2011-05-12 63 views
1

我是編程的總諾貝爾,請耐心等待。我的問題可能看起來很愚蠢。如何在PHP中查詢字符串中刪除不必要的字符串?

我這裏有一個查詢字符串:

http://localhost/exercises/php_js/exer_test/?year_list=1994&event_list=Singing+Contest&btn_show=Show

我覺得&btn_show=Show有事情做與提交元素的值。有沒有一種方法可以刪除或隱藏查詢字符串或URL?


index.php

<?php include('functions.php'); ?> 

<html> 
<head> 
<script type="text/javascript" src="myscripts.js"></script> 
</head> 
<body> 
<div> 
    <form name="myform" > 
    Select Year: <?php echo hspacer(1); ?> 
    <select id="year_list" name="year_list" > 
    <?php 
    for($year = (date('Y') - 100); $year <= (date('Y') + 100); $year++) { 
    if ($year == date('Y')) echo "<option value='$year' name='$year' selected='' >" . $year . "</option>"; 
    else echo "<option value='$year' name='$year' >" . $year . "</option>"; 
    } 
    ?> 
    </select> 
    <?php echo hspacer(5); ?> 
    Select Event: <?php echo hspacer(1); ?> 
    <select id="event_list" name="event_list" > 
    <?php 
    $events = array("Karate Tournament", "Beauty Pageant", "Film Festival", "Singing Contest", "Wedding"); 

    foreach($events as $event) echo "<option value='$event' name='$event' >" . $event . " </option>"; 
    ?> 
    </select> 
    <?php echo vspacer(2); echo hspacer(22); ?> 
    <input type="submit" id="bnt_show" name="btn_show" value="Show" onclick="show(); "/> 
    </form> 
</div> 
</body> 
</html> 



functions.php

<?php 
function hspacer($num_of_spaces) { 
    $spaces = ""; 
    if ($num_of_spaces > 0) for($i=0; $i<$num_of_spaces; $i++) $spaces .= "&nbsp;"; 

    return $spaces; 
} 

function vspacer($num_of_linefeeds) { 
    $linefeeds = ""; 
    if ($num_of_linefeeds > 0) for($i=0; $i<$num_of_linefeeds; $i++) $linefeeds .= "<br />"; 

    return $linefeeds; 
} 
?> 



myscripts.js

function show() { 
var year = document.getElementById('year_list').value; 
var event = document.getElementById('event_list').value; 
var result = "Year: " + year + "\nEvent: " + event; 

alert(result); 
} 
+0

我想你正在尋找'$ _SERVER ['QUERY_STRING']'。你可以在這裏找到文檔:http://www.php.net/manual/en/reserved.variables.server.php – styfle 2011-05-12 07:46:41

+0

感謝曼的信息... – 2011-05-12 07:56:31

回答

6

您可以刪除名稱(例如name="btn_show")以刪除URL中的查詢字符串。

<input type="submit" id="bnt_show" value="Show" onclick="show(); "/> 
1

只需通過添加方法參數來完成表單標記。例如。 method="post"。如果這樣做,表單中的變量將通過HTTP POST傳遞給Web服務器。之後,您可以在$_POST陣列中找到PHP中的變量。

查看以下網址以獲得完整的FORM標籤說明。

http://www.w3schools.com/tags/tag_form.asp

1

是的,如果你沒有使用的價值,從提交按鈕的輸入元素刪除值,或使用按鈕標籤。

如果您使用該值,則將它放入網址也是正確的。