0

So the problem is that i made a form on inde1.php and by using javascript i post data to login.php.

What i am trying to accomplish right now is when the login.php is executed and everything is ok to redirect user to another page.

on inde1.php the js is like this:

$(document).ready(function() {
      $("#myform").validate({
        debug: false,
        submitHandler: function(form) {
          $.post('login.php', $("#myform").serialize(), function(data) {
            checkFields(data);
            if(data!="ok")
            {
                 $("#myform")[0].reset();
                 grecaptcha.reset();
            }
            else
            {
                window.location.href = "pagetoredirect"
            }

          });

          return false; // if default form submission was not blocked, add this.
        }
      });
});

I was wondering if i could skip this part:

else
{
     window.location.href = "pagetoredirect"
}

and redirect users from login.php after login.php sends the "ok" to index1.php

Argiris
  • 189
  • 2
  • 15
  • [How to make a redirect in PHP?](http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) – Hunan Rostomyan Nov 28 '15 at 09:17
  • no you cant't. better to submit the form directly to `php` without using `$.post` – roullie Nov 28 '15 at 09:18
  • You need to validate your data in PHP and it is better than javascript validation and also necessary.After javascript validation send the data to php then validate it and for success and failure you can redirect the user to different page or same page with different messages . – Prafulla Kumar Sahu Nov 28 '15 at 09:20
  • well the validation is done on login.php, so you suggest that i should keep it as it is? with the ( window.location.href = "pagetoredirect" )? – Argiris Nov 28 '15 at 09:22
  • U can't skip that part if you are using this code, but is there any reason why you want to? – Mark Ng Nov 28 '15 at 10:17
  • @MarkNg Why i want to redirect through php and not with that js that i already have? (if that is your question), well i was just wondering if it is possible, cause i would prefer to keep redirect links as much as possible hidden. Don't know if i am wrong anywhere, if so please correct me. – Argiris Nov 28 '15 at 10:21
  • you mean you do not want people to see in on the url? or you do not want when people open the console they can see your js? – Mark Ng Nov 28 '15 at 10:24
  • eventually people can still see the url in the address bar when redirected, or you can use js to return to a div wrapper or use iframe? still don't understand what is your expected result – Mark Ng Nov 28 '15 at 10:37
  • Well yes you are right that people eventually can see the redirect link, but i was wondering if it is possible to redirect them from php before the script sends anything back to the inde1.php (cause right now in order to redirect i have to echo "ok";) – Argiris Nov 28 '15 at 10:45
  • Yes, use script to submit and redirect by the called php script, but its a 1 way, then you will lose your argument in your js for grecaptcha.reset or reset frm, then it makes more sense to just php. – Mark Ng Nov 28 '15 at 11:08

1 Answers1

1

well you can use header function it should be like lets say a page with a button on it

<form action='login.php' method='post'>
<input type='submit' value='submit' name='submit'>
</form>

and than on login.php page you can check if the button is pressed or not

<?php 
//for using header function it is important to use ob_start at the starting of php
ob_start();
if(isset($_POST['submit'])
{
//than use the header function
header ('location: profile.php')
//you will not even notice the opening of login.php and directly redirected to profile.php
}
?>
rohitsaroha125
  • 126
  • 1
  • 2
  • 16