0

I need to submit a form without reloading or even interrupting the page. The "submit" input should only start a php code where a sql INSERT query gets executed.

This is what I use at the Moment:

echo "
    <form method='post'>
        <input type='hidden' visibility='hidden' value='hiddenID' name='userid' id='userid'/>
        <textarea class='form-control' rows='5' name='newmessage' id='newmessage'></textarea><br/>
        <input class='btn btn-primary' type='submit' name='submit_chatmessage' value='Send'>
    </form>";


    if($_POST[newmessage] == ""){

    } else {

        date_default_timezone_set('Europe/Berlin'); // CDT

        $current_stamp = date('Y-m-d H:i:s');

        $sql="
        INSERT INTO tbl_messages (`tbl_users_ID`, `time`, `message`, `from_admin`) VALUES ($_POST[userid],'$current_stamp','$_POST[newmessage]',1)";

        mysql_query($sql);
        }
    }

I already tried things like an onsubmit handler or action="javascript:void(0);" but that wasn't working. There was no new data in the database so the query wasn't executed.

If I submit the form normally, the query gets executed as expected, but the page reloads.

1 Answers1

0

You coud say, that the php script starts when the INSERT was successful and if the button got already pressed:

if((mysql_query($sql)) && (isset($_POST['submit_chatmessage']))) {
 ...
}

(mysql_query() should return true or false)

Benito
  • 23
  • 2
  • 3
  • 9