Php post form without refresh page

284
January 12, 2018, at 00:15 AM

I try to post my form to Mysql without refreshing page. I did these with looiking sources but not working. Could you help me?

<script>
$('#submit').click(function() {
    $.ajax({
        url: 'submit.php',
        type: 'POST',
        data: {
            message: '*I couldnt find this partwhat should i write*'
        }
        }               
    });
});
</script>
<form method="post">
<textarea name="message" rows="3" cols="30">
</textarea><br><br>
<input type="submit" value="Submit">
</form>

Submit.php

<?php
include "connect.php";
if(isset($_POST['message'])) {
    header('Location: ' . $_SERVER['HTTP_REFERER']);
    $post = $_POST['message'];
    $date = date("y-m-d G:i:s"); 
    $query = $db->prepare("INSERT INTO chat_messages SET senderid = ?, receiverid = ?, message = ?, mod_time = ?"); 
    $insert = $query->execute(array( $a, $b, $post, $date));
}?>
Answer 1

You are submitting form data through Ajax query, hence you do not need to include header('Location: ' . $_SERVER['HTTP_REFERER']); in your submit.php file.

<form>
<textarea name="message" rows="3" cols="30"></textarea>
<br>
<input type="submit" value="Submit">
</form>
<script>
    $(document).ready( function() {
        $("form").on("submit", function(e) {
            e.preventDefault(); // Prevent default form submission action
            $.post("submit.php", $("form").serialize()); // Post the data
            $('textarea[name=message]').val(''); // Clear the textarea
        });
    });
</script>

Also, are the variables $a and $b defined in submit.php file?

Answer 2

In jQuery, the click event is being triggered on an element that has an id of submit (it is id because it is represented by #)

$('#submit').click(function() { 

Your submit button does not have the ID of "submit"

Change the input tag as follows:

<input id="submit" type="submit" value="Submit" />

Another problem, as @Rajan in comments pointed out, you have an extra brace. So, change:

data: {
    message: '*I couldnt find this partwhat should i write*'
}
}

to:

data: {
    message: '*I couldnt find this partwhat should i write*'
}

Also, I recommend that you show return some kind of message from submit.php page, for example:

echo 'Entry Added';

The above is just an example output to get you going... you really should be doing checks such as: did the entry get inserted without any errors, etc.

Edit

Also note: you are using type as one of the settings. Per the official jQuery documentation of jQuery.ajax(), type is:

An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0.

(i.e. use method instead, if using jQuery version >1.9.0)

Lastly, take a look at the answer provided by @Faisal as well...

Answer 3

$.post('../submit.php',{message:message}, function(data) { 
       
      $('.results').html(data); 
   });

use a div where you want to display the result

<div class="results"></div>

to finish your submit.php have to send something at the end so try this

<?php 
include "connect.php"; 
if(isset($_POST['message'])) { 
    header('Location: ' . $_SERVER['HTTP_REFERER']); 
    $post = $_POST['message']; 
    $date = date("y-m-d G:i:s");  
 
    $query = $db->prepare("INSERT INTO chat_messages SET senderid = ?, receiverid = ?, message = ?, mod_time = ?");  
    $insert = $query->execute(array( $a, $b, $post, $date)); 
} 
echo "it works"; 
?>

Rent Charter Buses Company
READ ALSO
Create four independent dropdown values using, jquey,azax,php and mysql [on hold]

Create four independent dropdown values using, jquey,azax,php and mysql [on hold]

I want to create four dropdown values which are dependent on previous select optionFor eaxample i have four select options like world,country,state, and city,then i want county is dependent on world,state dependent on country,city dependent on state

209
Import Apple iTunes Affiliate Data into PostgreSQL?

Import Apple iTunes Affiliate Data into PostgreSQL?

I am attempting to download and ingest data from Apple's Enterprise Partner Feed into a PostgreSQL databaseI have found that Apple provides a tool called EPFImporter that is designed to import the data into a MySQL database

187
Subtracting two rows in the same column in mysq

Subtracting two rows in the same column in mysq

Hi i am having a table with the name SourceHere i have a data as shown below

241
timediff between rows

timediff between rows

I have to following query below fig1 , which works, but what I would like to do is get the timediff between row 4 and row 5 then get the difference between row 5 and row 6

250