Answer the question
In order to leave comments, you need to log in
How to display title ajax?
Good afternoon. I send data from the form to the database, I don’t know how to display the title of the form immediately on the page via ajax.
<div class="display-flex" style="display: flex;">
<div class="from" style="margin-right: 200px;">
<input type="text" name="title" class="title"> <br>
<textarea name="content" class="content" id="" cols="30" rows="10"></textarea> <br>
<button type="button" class="button">Submit</button> <br>
</div>
</div>
<script>
$(document).ready(function(){
$('button').on('click', function(){
var titleValue = $('input.title').val();
var contentValue = $('textarea.content').val();
$.ajax({
method: "POST",
url: "some.php",
data: { title: titleValue, content: contentValue }
})
.done(function( ) {
});
$('input.title').val('');
$('textarea.content').val('');
})
});
</script>
<?php
require 'db.php';
$data = [
"title" => $_POST['title'],
"content" => $_POST['content']
];
$sql = 'INSERT INTO posts (title, content) VALUES (:title, :content)';
$stmt = $connect->prepare($sql);
$result = $stmt->execute($data);
var_dump($result);
Answer the question
In order to leave comments, you need to log in
You need to check the result of the write in PHP and, if successful, return a confirmation in response to the post request.
Then, in you process the response.
For example, like this:.done(function() {...});
.done(function(rez) {
if (rez.isDone) {
$('input.title').val(titleValue);
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question