<body>
<from action=?>
<input type="text" name="test" id="test">
<input type="submit" name="button" id="button">
<from>
<?php
function test($test){
echo "<script> alert("Hello World"); </script>";
}
?>
</body>
1. 有辦法 使用from 的 action 呼叫 php function 嗎?
我是想要把 text 的資料傳送到 function 內做處理
2. 在 php echo 內,可以呼叫 javascript 的 alert 嗎?
或是 PHP 有類式 mesgbox 的視窗 ?
Copyright © 2024 1QUIZZ.COM - All rights reserved.
Answers & Comments
Verified answer
原生PHP是沒有直接操作上兩點的功能的,但當你了解PHP及HTML的方式後,就可以自行把上述功能用小技巧完成,另外就是使用現成的PHP Framework,會有許多較強大的功能,但也不一定是您想要的樣子。
1、form是html,因此action就是只能送給網頁,例如test.php
要呼叫function的話,可以附加一個hidden欄位例如:
<form action="test.php method="post">
...
<input type="hidden" name="action" value="test">
</form>
<?php
function test()
{
...
}
if ($_POST['action'] == 'test')
{
test();
}
?>
2.javascript的 alert或想要messagebox,基本上都屬於dhtml(javascript, css html)的部份,但兩個是相同的意思。
原則上php就只能echo,產生dhtml的程式碼,但可包裝成function。
如:
<?php
function MessageBox($msg)
{
echo("<script>alert('".$msg."');</script>");
}
MessageBox('hello');
?>
如上alert在javascript中也可替換為其它已寫好的 messageBox。
之前有需求的時候 自己寫了一個混搭式的寫法 = =
雖然不是很正式 ....但是我用的還不錯 你看看囉
function msgbox($msg,$position){
?>
2010-04-02 00:44:54 補充:
function msgbox($msg,$position){
?><script language="javascript">
{
alert('<?=$msg?>');
location.replace('<?=$position?>');
}
</script>