[英]passing post array to function
我怎樣才能將一個發布數組傳遞給一個函數,然后拉出參數,即
所以我發布以下內容。
url=http%3A%2F%2Fwww.facebook.com%2Ftest&userId=680410999&location=Cardiff&lang=en_US&social=facebook
我可以從功能中像這樣抓住它嗎?
function login($_POST){
//then output the var her.
$_POST['url']; etc
}
這是為了節省我在ajax.php文件中執行以下操作的過程
function login($_POST['url'],$_POST['userId'],$_POST['location'],$_POST['lang']){
}
任何幫助
首先,您應該使用$_GET
而不是$_POST
。 其次,您不需要將函數參數稱為$_GET
。 你可以這樣做:
function login($array)
{
// do stuff with array
}
然后以這種方式調用該函數:
login($_GET);
另一個建議是閱讀以下引用,這是實際參數與形式參數的區別:
形式參數是函數定義中已知的參數。 實際參數(也稱為參數)是調用方傳遞的參數。
如果我對您的理解不正確,則需要一個函數,然后將其作為參數傳遞給數組(在您的情況下為$_POST
或$_GET
數組)。
因此,您的函數將如下所示:
// not sure what you're doing with your login options but here's an example of getting them out of the array and putting them in variables - using the url as an example here...
var url;
function login($loginOptions) {
// set the url from the array
url = $loginOptions["url"];
// do the same for all your other variable...
}
然后,您將這樣調用函數:
login($_POST); // or $_login($_GET); depending on what you're using
就是這樣!
您得到的其他答案應該可以為您解決此問題。
僅提供一些建議,請記住清除代碼中使用的所有$_POST
和$_GET
數據。
例如,如果在數據庫查詢中使用它,則將需要使用mysql_real_escape_string();
如果回顯數據,則使用htmlentities();
我認為您的意思是為此將數組解析為函數,我將告訴您使用函數並將其與SESSION連接,如下所示。 您可以使用$ _GET [$ var]來檢索變量,但是get方法很難處理,可以使用$ _POST方法並將其與會話結合起來以保存數據,我將說明如何協作對每個頁面進行會話,以便您可以輕松檢索所需的變量。 這是代碼:
<?php
session_start();
if(isset ($_POST['postedit']))
{
$_SESSION['url'] = $_POST['urlsend'];
header("location:dua.php");
}
?>
<html>
<body>
<form action="satu.php" method="POST">
Name : <input type="text" name="urlsend"> <br> <br>
<input type="submit" name="postedit" value="SEND DATA">
</form>
</body>
</html>
<?php
session_start();
if(isset ($_SESSION['url']))
{
//if data empty then will be redirected to the previous page
echo $_SESSION['url'];
}
?>
我創建的都是動態數據,即使您可以選擇使用此方法還是其他方法。 我希望你能夠明白。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.