2012-12-18 94 views
0

大家..形式沒有提交PHP和HTML

試圖提交表單..和形式和PHP代碼是在同一個index.php文件

if(isset($_POST['post_form'])) { 
    echo "ISSET"; 

    $post_sender=$id; 
    $post_reciever=$id2; 
    $post_cont=$_POST['news_text']; 
    $post_cont=htmlentities($post_cont); 
    $post_cont=mysql_real_escape_string($post_cont); 

    $post_sql=mysql_query("INSERT INTO wall_posts (from, to, content, date) VALUES ('$post_sender','$post_reciever','$post_cont',now())") or die(mysql_error()); 

    }else{ 
     echo "NOT SET"; 
    } 

HTML表單

<form method='POST' action='index.php' enctype='multipart/form-data' id='news_form' name='post_form' > 
<textarea id='news_text' name='news_text' >type here..........</textarea> 

<input type='submit' id='wall_post_btn' name='wall_post_btn' value='submit'> 

</form> 

哪裏是我的錯誤..?

沒有在code..the文件本身corrupted..just取得了一個新的PHP文件中的測試沒有錯......只是罰款...感謝傢伙..

+0

有什麼問題,有什麼不行? –

+0

我不認爲表單本身實際上會發送POST數據。如果您參考了其中一個文本元素,它會起作用嗎? 'if(isset($ _ POST ['news_text'])){' –

回答

0

問題是要檢查存在於$_POST的形式,這是從來沒有存在的名稱...

什麼,你應該測試是提交按鈕的名稱,例如:

if(isset($_POST['wall_post_btn'])) { 

下一頁你可以使用一個線消毒輸入:

$post_cont= mysql_real_escape_string(htmlentities($_POST['news_text'])); 

還有最後一:開始使用PDO預處理語句或至少mysqli_*功能mysql_*那些正在棄用...

+0

仍然是新的PHP ...陳述與經典.. 我知道我必須移動到PDO ...謝謝 – Alex

1

$ _ POST [ 'wall_post_btn']。提交的名稱,而不是表格

0

您不應該使用mysql,因爲它已折舊。看看庫MySQLi或PDO

此:

"INSERT INTO wall_posts (from, to, content, date) VALUES ('$post_sender','$post_reciever','$post_cont',now())" 

應該是:

"INSERT INTO wall_posts (from, to, content, date) VALUES ('".$post_sender."','".$post_reciever."','".$post_cont."',now())" 
+0

不,它不應該**,它**可能** ...使用'「Hello mr。$ name 。'',並且不需要''Hello'先生'。$ name' ...如果他使用了''',那麼他將不得不按照這種方式拆分字符串:''Hello'先生'。$名稱「,使其工作... – shadyyx

+0

可變插值(VS)連鎖的終身難題。所有這些都會很好,直到你需要$ name的日子不再是一個變量。總是有10種不同的方式來獲得相同的結果,有些只是比其他方式更好。 –

2

post_form是形式的名稱。您必須檢查提交按鈕,0​​。

if(isset($_POST['wall_post_btn'])) { 
    // entire code here 
} 
+0

@Jeremy謝謝你打磨我的答案。但是,所有應有的尊重,我想告訴你,他/她已指定post_form作爲表格的名稱。 –

+0

對不起。我錯過了原來的帖子。 – Jeremy1026

+0

仍然沒有變化 – Alex

0

有兩個問題:

  1. 您需要連接到你的MySQL服務器使用mysql_real _...
  2. mysql_real _...已被棄用,並且從PHP中移除

看看這裏的替代品: http://php.net/manual/en/function.mysql-real-escape-string.php

+0

也許他在'if'語句之前有一個有效的連接。無論如何,這不是真正的問題,爲什麼它不工作... – shadyyx

+0

我已經連接.. – Alex

0

使用此代碼的工作正常

<?php 
if($_SERVER['REQUEST_METHOD']=="POST") { 
    echo "ISSET"; 
    $post_sender=$id; 
    $post_reciever=$id2; 
    $post_cont=$_POST['news_text']; 
    $post_cont=htmlentities($post_cont); 
    $post_cont=mysql_real_escape_string($post_cont); 

    $post_sql=mysql_query("INSERT INTO wall_posts (from, to, content, date) VALUES ('$post_sender','$post_reciever','$post_cont',now())") or die(mysql_error()); 

    }else{ 
     echo "NOT SET"; 
    } 
?> 
<form method='post' action='index.php' enctype='multipart/form-data' id='news_form' name='post_form' > 
<textarea id='news_text' name='news_text' >type here..........</textarea> 

<input type='submit' id='wall_post_btn' name='wall_post_btn' value='submit'> 

</form>