简体   繁体   中英

Form is not submitting php and html

everyone..

trying to submit a form.. and form and php code is in the same file 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

<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>

where is my mistake..???

there is no mistake in the code..the file itself is corrupted..just made a test in a new PHP file...works just fine...thanks guys..

post_form is the name of form. You must check for the submit button, wall_post_btn .

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

$_POST['wall_post_btn']. the name of the submit, not the form

You should not use mysql as it has been depreciated. look into mysqli or PDO

This:

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

should be:

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

There are two problems:

  1. You need to connect to your mysql server to use mysql_real_...
  2. mysql_real_... is deprecated and is being removed from php

Take a look here for alternatives: http://php.net/manual/en/function.mysql-real-escape-string.php

The problem is You are checking the name of the form present in the $_POST , which is never present...

What You should test is the name of the submit button, eg:

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

Next You could use one line for sanitizing the input:

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

And last one: start using PDO with prepared statements or at least mysqli_* functions as mysql_* ones are deprecated now...

Use this code its working fine

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM