[英]Php and forms problem
這是我的超級簡化索引。php:
<?php
require_once 'DeleteOrAdd.php'; // handles adding/deleting a db record
doAddDeleteRecord();
// other functions are called here, left out though for brevity
?>
這是 DeleteOrAdd.php(非常簡化)
<?php
function doAddDeleteRecord()
{
echo <<<_END
<form action="index.php" method="post">
// the other form html not shown here
<input type="submit" value="ADD RECORD" />
</form>
_END;
// NOT SHOWN -- code to handle the form when it is POST'd
}
?>
所以現在是晚上 10 點 30 分,我是 PHP 的新手,好吧/借口。 無法弄清楚如何做到這一點。
我想將上面的form action="index.php"
更改為form action="DeleteOrAdd.php"
(即,我想重新發布到該表單所在的同一文件,而不是index.php,所以代碼更干凈)。 但它不起作用,因為我有 POST 的所有表單處理邏輯——在doAddDeleteRecord()
function 中,所以如果我設置我的表單action="DeleteOrAdd.php"
它將不起作用。
是否可以執行form action="DeleteOrAdd.php:doAddDeleteRecord()
類的操作?
我不想把這個放在課堂上。 我還想保留我的 index.php 就像上面一樣——調用函數,除此之外沒有主要的內聯代碼。
有任何想法嗎?
Originally, all the code was inline inside index.php (got it from a PHP book's sample) and I then divided the code into logically-named PHP files in the Netbeans project to clean it up, and to put stuff in functions that get called來自索引.php。
從表單中完全刪除操作值,默認情況下它將始終返回到顯示它的 url。
<form action="" method="POST">
您的應用程序結構不合理。 我建議遵循MVC模式。
但是對於您當前的問題,您可以執行以下操作
只需將操作設置為您的<form action="DeleteOrAdd.php"
或者您可以將操作完全留空,從而將您的數據發布到創建表單的同一文件中。
發布表格后,您可以在DeleteOrAdd.php
文件中執行以下操作。
if (isset($_POST['submit']))
{
doAddDeleteRecord();// this will call your
}
但在這種情況下,您可能必須更改index.php
的代碼
我認為您在這里遇到的問題是能夠讓您的 PHP 頁面辨別其是否是新加載或是否提交表單,這就是您將索引頁面合並到您的操作參數中的原因。 但是,這不是必需的。
將提交元素的 id 和 name(用於有效標記)屬性設置為唯一名稱。 比如“form_submit”所以這里是一個例子。
<form action="" method="post">
<input type="submit" id="form_submit" name="form_submit" value="ADD RECORD" />
</form>
所以你在你的 PHP 腳本(doAddorDelete.php)中放的是這個......
if (array_key_exists('form_submit', $_POST)) {
//this is the code to execute on form submit
//use print_r($_POST) to view variables you can use here
//make sure you validate all data passed here especially if using a database
//ie if MySQL
//$validated_userinput = mysql_real_escape_string(strip_tags(htmlentities(trim($_POST['userinput']))), $link_resource); for text
//(int) $_POST['userinput']; for numbers
} else {
echo <<<_END
<form action="" method="post">
// the other form html not shown here
<input type="submit" id="form_submit" name="form_submit" value="ADD RECORD" />
</form>
_END;
}
希望這可以幫助: :)
前言:既然你說這是一個學習練習,我將跳過關於最佳實踐的偽善宣言以及 OOP 的眾多優點。 ;) 你的書可能詳細說明了我通常會在這樣的解決方案之前提出的每一個可怕的警告/嚴厲的演講。
是否可以執行表單 action="DeleteOrAdd.php:doAddDeleteRecord() 之類的操作?
簡而言之,是的。 實現目標的最簡單方法是在表單操作中引用您的文件,就像您所做的那樣:
<!-- form.php -->
<form action="DeleteOrAdd.php" method="POST">
然后在 DeleteOrAdd.php 中,通過測試您的表單提交將發送的 $_POST 數據來觸發您的 function,如下所示:
<?php
// DeleteOrAdd.php
if(isset($_POST['some_form_variable']) && $_POST['some_form_variable'] != null) {
$data = array();
foreach($_POST as $post) {
array_push($data, $post);
}
doAddDeleteRecord($data);
}
function doAddDeleteRecord($data) {
// ...your processing code, etc.
坦率地說,您指定的純程序方法的結果是,您可以做這樣的事情。 你不會想在現實生活中像這樣發展(也跳過這個深入研究,我保證你的書解釋了為什么不詳盡的細節。)
重要的提示!! 由於我在您發布的代碼片段中沒有看到返回值,並且您說您才剛剛開始,因此我將花一點時間指出一個隱藏的陷阱,以防萬一:
--> 你的代碼可能與我在你的 function 上方添加的那六行完美配合,如果你不知道,你永遠不會知道
捕獲所述值,以便您可以對其采取行動/顯示它/以其他方式向自己展示
一個。 發生了一些事情——理想情況下,
灣。 那是什么東西。
否則,您所得到的只是模棱兩可:沒有跡象表明它正在工作或正在破壞(不拋出錯誤、警告等)。 至少可以說,調試令人沮喪。
所以,也就是說——假設你的 function 返回了一些東西(成功時為真,帶有消息的字符串,無論如何)它可能是這樣的:
function doAddDeleteRecord($data) {
// ... your function code, etc.
$sql = "INSERT INTO mytable VALUES(".implode(',',$data).")";
if (mysql_query($sql) == true) {
$message = "Record saved";
} else {
$message = false;
}
return $message;
}
您的 function 返回的任何值都需要一個變量來捕獲它,否則它將不會被設置。 當您調用 doAddDeleteRecord() function 時,使用變量賦值捕獲它:
... // same 6 little lines of conditional code ...
}
$result = doAddDeleteRecord($data);
}
// maybe just echo it out or something...
echo $result;
- 或者 -
... // still the same 6 lines ...
}
$result = doAddDeleteRecord($data);
}
// maybe have a new test based on the outcome of the last one...
if ($result == false) {
// do something about the fail...
} elseif (is_string($result)) {
// do something about the success...
}
祝你好運,HTH。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.