看板 PHP 關於我們 聯絡資訊
小弟最近開始接觸php 想練習寫個聊天室 現在碰到一個問題想來請教一下 <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>sent</title> </head> <body> <script> function insertDB(){ <?php $meg = $_POST['megword']; $str2 = $_SESSION['name']; $sqlinsert = "INSERT INTO chat( nick, words ) VALUES( $str2, fde );"; echo "alert( '$meg' )"; // $result = mysql_query($sqlinsert) or die('MySQL query error'); ?> } </script> <form mothod = "post" > <input name="megwords" type="text" size="20" maxlength="20" /> <input name="sent" type="button" onclick="javascript:insertDB();" value="送出 " /> </form> </body> </html> 基本上這是聊天室中輸入訊息按傳送的頁面 按出傳送以後我想在這個頁面做新增到資料庫的處理 新增要送出的訊息跟使用者暱稱 使用者暱稱在前面已經用session存取了 可是這個要傳出的訊息我抓不到 怎麼弄好像都不對 想來請教一下各問板友 <input name="megwords" type="text" size="20" maxlength="20" /> 該死的這行看著他沒辦法囧rz -- ◢ ◢ ∕ |╴╴╱ ︱ 皮卡丘眉頭一皺 覺得事情並不單純...... ∣ | ∣ ㄟˇㄏ | /╲ | ㄧ..ㄧ | / / |○/ ︷ \ ○| ︳ / -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.43.61.11 ※ 編輯: terryone 來自: 114.43.61.11 (11/12 23:21)
FMDream:$_POST['megword'] 然後 input name="megwords" ???? 11/12 23:21
terryone:樓上是說在insertdb裡面使用POST[megwords]嗎? 11/12 23:22
terryone:這我嘗試過了 抓不到 11/12 23:22
FMDream:你先print_r($_POST) 看看吧 11/12 23:23
FMDream:你input name是megwords 但卻抓$POST['megword'] 11/12 23:25
FMDream:加上 你確定你的onclick的動作會POST資料出去嗎? 11/12 23:28
terryone:眼殘@@ 不過改回POST[megwords] 一樣抓不到 11/12 23:28
terryone:這也是我的疑問 我不知道這樣的情到底該怎麼抓資料 11/12 23:29
FMDream:google "form submit" 11/12 23:31
BloodyDawn:mothod?改成method呢? 11/12 23:57
terryone:改了一樣抓不到 11/13 00:01
我爬文的結果應該是button觸發javascript 會導致POST接不到東西 所以我改成 <form method = "post" action = "自己這一頁!!.php" > <input name="megwords" type="text" size="20" maxlength="20" /> <input name="sent" type="submit" value="送出 " /> ^^^^^^^ button改submit 就順利的接到東西了 上面的function直接拔掉function包覆的部分 直接<?php function內容 ?> 就都完成我的需求了 不知道我上面的理解有無錯誤的地方 請指正 ※ 編輯: terryone 來自: 114.43.61.11 (11/13 00:19) ※ 編輯: terryone 來自: 114.43.61.11 (11/13 00:20)