Goal: I want to Hide "myForm" div when data successfully post to the database and the "section" div will show component when submit button will clicked, its working properly.
Problem / Requirement: Problem is that data not posting to the database, how can i solve this problem to save data to the database and show / hide div after that database operation.
Note: I don't want to reload my page.
HTML Code:
<div id="section" style="display: none;">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestiae cupiditate culpa reprehenderit animi,
numquam distinctio repellendus debitis fugit unde consequatur eum magni illo minima amet quidem omnis veniam
commodi voluptatum!
</p>
</div>
<div id="myForm">
<form id="testForm" method="post" action="#">
<input id="form_name" type="text" name="name" class="form-control">
<input type="submit" name="submit" class="btn btn-send" value="submit">
</form>
</div>
JavaScript Code:
const sectionDiv = document.getElementById('section');
const form = document.getElementById('myForm');
form.addEventListener('submit', function (e) {
e.preventDefault();
// Please suggest the flow and code for call PHP script from here
form.style.display = 'none';
sectionDiv.style.display = 'block';
});
Please suggest flow and process in above Javascript code for how to pass my data to below PHP script..
PHP Code:
include("db.php");
if (isset($_POST['submit']))
{
$form_name= $_POST['name'];
$query = "INSERT INTO `test` (`xxname`) VALUES ('$form_name')";
if(mysqli_query($conn, $query))
{
echo "asdfghjkl";
}
else
{
echo "ERROR: Could not able to execute $query. " . mysqli_error($conn);
}
}
Thanks for your help in advance..