<?php
session_start();
include_once "connect.php";
$fullName = $_POST['fullname'];
$userName = $_POST['username'];
$emailAdd = $_POST['email'];
$passWord = $_POST['password'];
$query = mysql_query("SELECT * FROM users where USERNAME = '$username' ");
$result = mysql_fetch_array($query);
if($fullName == "")
{
?>
<script>
alert("Full Name is required!");
window.location.href = "registeruser.php";
</script>
<?php
}
else
{
if ($userName == "")
{
?>
<script>
alert("Invalid username!");
window.location.href = "registeruser.php";
</script>
<?php
}
else
{
if($userName == $result['USERNAME'])
{
?>
<script>
alert("Username already exists!");
window.location.href = "registeruser.php";
</script>
<?php
}
else
{
if ($emailAdd == $result['EMAIL'])
{
?>
<script>
alert("Email address is required!");
window.location.href = "registeruser.php";
</script>
<?php
}
else
{
if ($passWord == "")
{
?>
<script>
alert("Username is required!");
window.location.href = "registeruser.php";
</script>
<?php
}
else
{
if($_POST['password']==$_POST['confirmpass'])
{
mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD)
VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')" );
mysql_close();
?>
<script>
alert("Registered Successfully!");
window.location.href = "index.php";
</script>
<?php
}
else
{
?>
<script>
alert("Password and Confirm Password do not match");
window.location.href = "registeruser.php";
</script>
<?php
}
}
}
}
}
}
?>
It works fine. But the problem is: the alert window shows to another web page and NOT within the webpage It is okay but it looks awful to direct the user to another web page without having contents there. If you could help me please on how to fix this and make the javascript alert window display on the same web page only. Actually, I have tried some solutions like this..
if($fullName == "")
{
echo '<script>';
echo 'alert("Full Name is required!")';
echo 'window.location.href = "registeruser.php"';
echo '</script>';
}
For displaying alert message after redirection you may use Session or QueryString and on page Load check if the Session or QueryString is not empty then display alert message. Check the example.
The alert() method in JavaScript displays an alert box with a specified message and an OK button. It is often used to make sure that information comes through to the user. The alert box takes the focus away from the current window and forces the browser to read the message.
Hello do you try to use ajax with jquery
now is your script just return a json object to say if all is ok //reg.php
session_start();
header('Content-type: application/json');
include_once "connect.php";
// result we return to the user
$response = ['error' => false, 'message' => ''];
$fullName = $_POST['fullname'];
$userName = $_POST['username'];
$emailAdd = $_POST['email'];
$passWord = $_POST['password'];
$query = mysql_query("SELECT * FROM users where USERNAME = '$username' ");
$result = mysql_fetch_array($query);
if($fullName == "") {
$response['message'] = 'Full Name is required!';
$response['error'] = true;
} else {
if ($userName == "") {
$response['message'] = 'Invalid username!';
$response['error'] = true;
} else {
if($userName == $result['USERNAME']) {
$response['message'] = 'Username already exists!';
$response['error'] = true;
} else {
if ($emailAdd == $result['EMAIL']) {
$response['message'] = 'Email address is required!';
$response['error'] = true;
} else {
if ($passWord == "") {
$response['message'] = 'Username is required!';
$response['error'] = true;
} else {
if($_POST['password']==$_POST['confirmpass']) {
mysql_query("INSERT INTO users (FULLNAME, USERNAME, EMAIL, PASSWORD) VALUES ('$fullName', '$userName', '$emailAdd', '$passWord')" );
mysql_close();
$response['message'] = 'OK';
$response['error'] = false;
} else {
$response['message'] = 'Password and Confirm Password do not match';
$response['error'] = true;
}
}
}
}
}
}
die(json_encode($response));
and // registration.php
<form method=POST action="reg.php" id="myForm">
<!--input goes here-->
</form>
<script src="//path to jquery"></script>
<script>
$(document).ready(() => {
$('#myForm').on('submit', function (e) {
e.preventDefault();
$.post('reg.php', $(this).serialize())
.done((datas) => {
if(datas.erreur) {
alert(datas.message);
} else {
alert('registration succesfully');
}
}).fail(() => {
alert('connect error !!!');
});
});
});
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With