I'm aware this question has been asked millions of times but hopefully this is slightly different as i can't seem to find an answer. I'm trying to redirect back to the index page (where login takes place) after a successful sign up process. Ideally
header("Location: index.php");
would be used. However, i can't use that as part of my PHP validation uses echo's. The code i have below works perfectly. Using
<script>
window.location.href = "index.php";
</script>
This is the end part of my validation code:
//loads of other form validation using echo's
...
{
$resultusername = queryMysql("SELECT * FROM members WHERE username='$username'");
if ($resultusername->num_rows)
{
echo <<<_END
<div id=usernameinuse>
<p>This username is already in use!</p>
</div>
_END;
}
else
{
$qresult = queryMysql("INSERT INTO members (firstname,
surname, gender, dob, email, username, password)
VALUES('$firstname', '$surname', '$gender', '$dob', '$email', '$username', '$password')");
echo <<<_END
<script>
window.location.href = "index.php";
</script>
_END;
}
Like i said this works perfectly, however, I'd ideally like all jquery and javascript to be in separate files from my php and linked at the top of the file (other jquery files are already up there) and want to avoid going into javascript in the middle of a php file. So my question is, is there a way to make that script external and catch it in a jquery file like
$('input[type=submit]').click && ($qresult)(function() {
window.location.href = 'index.php';
return false;
}
This works without the && ($qresult)
bit, but then if you just click submit, non of the validation takes place and nothing is entered into the database.
I'm also aware PDO is a lot better to use and mysql functions are slightly obsolete but i'm just getting used to everything. Thanks
Lets say you have a controller file that has this function:
function insert_user($data) {
$qresult = false;
$resultusername = queryMysql("SELECT firstname FROM members WHERE username='$username'");
if ($resultusername->num_rows < 1)
{
$qresult = queryMysql("
INSERT INTO members (firstname, surname, gender, dob, email, username, password)
VALUES('{$data['firstname']}', '$data['surname']', '$data['gender']', '$data['dob']', '$data['email']', '$data['username']', '$data['password']')
");
}
return $qresult;
}
If your form action sends a POST request to this same file, where you'll be calling insert_user() something like this:
$inserted = insert_user($_REQUEST);
if($inserted) {
include("successful_registration.php");
}
else {
include("user_already_exists.php");
}
where these two included files are two different views. You may want to tweak this logic to your liking but the idea here is to separate your business logic from view logic. If not using include as I suggested, you may be able to redirect to different pages using header() before you even get to rendering and views.
If you are using ajax to insert a user then again a POST to controller will give you back the information you need in client side to decide what you want to do (eg. manipulate content, re-render a part of the page, etc).
This may be just an opinion, but separation of query code from view code is the first step to getting this problem solved correctly.