-2

In PHP JavaScript window.location.href is not working on submit button. The problem is that function order() is not going on submit button. I have tried alert(''); message also.

I want when order button submitted, then form submits and user is redirected to login.php

<script type="text/javascript">
  function order() {
    var sStr1 = "login.php";
    window.location.href = sStr1;
    return false;
  }
</script>

<input type="submit" class="button4" name="order" id="order" value="Place Order" onclick="order()" >
JFK
  • 40,963
  • 31
  • 133
  • 306
  • What output did you get on submit? – Harish Ambady Mar 04 '13 at 07:46
  • Try using onclick="javascript:return order()" – Sudip Pal Mar 04 '13 at 07:48
  • @HarishAnchu nothing just form submitted onclick submit –  Mar 04 '13 at 07:51
  • Hmm if you aren't actually submitting a form why are you using a submit button? – James Mar 04 '13 at 07:53
  • problem is that function order() not going on submit button even i have tried alert(''); message also –  Mar 04 '13 at 07:53
  • @james i want when order button submit then form submit and user redirect to login.php –  Mar 04 '13 at 07:54
  • @AkberHussain What's your "form action", if you want to submit the form then why are you doing it like this. Submit the form normally by providing the form action and from that page navigate the user to login page. – Ravinder Singh Mar 04 '13 at 07:58
  • is the function being called...try adding a alert as first line, and also check if the new href location is valid – A J Mar 04 '13 at 08:01
  • This used to be possible but I believe now it doesn't work at all in any browser. Either AJAX your form and then load the new URL or have your server-side code redirect the browser after it has processed the form data. – James Mar 04 '13 at 08:01
  • @aj alert message also not working –  Mar 04 '13 at 08:02
  • @RavinderSingh form action="confirm.php"> in confirm.php there is mail function code –  Mar 04 '13 at 08:03
  • @AkberHussain Do you think that this javascript function will submit the form then you are wrong, this can only navigate the user to login page and use window.location – Ravinder Singh Mar 04 '13 at 08:03
  • Just don't call the order function onclick on submit function, navigate the user from confirm.php page. – Ravinder Singh Mar 04 '13 at 08:04
  • @AkberHussain: that means the call itself is not working, try onclick="var x = order()" in the submit tag. I m not sure but might work since it constraints the false to the var x. – A J Mar 04 '13 at 08:30
  • thats wierd..the function itself is not gettin invoked!!!try removing the return part and see if u get an alert then. – A J Mar 05 '13 at 13:32

2 Answers2

0

Changing window.location does not submit a form. I'd rather type <form action="login.php" method="..."> and then Javascript is not necessary.

Voitcus
  • 4,463
  • 4
  • 24
  • 40
  • javascript is neccessary because when submit button click then form will go to another page –  Mar 04 '13 at 07:58
  • I don't understand. If you want to submit the form, you just click the submit button and the browser does redirecting to the file specified in `action` attribute and sends all form fields. If you change `window.location` you will redirect but no sending occurs. – Voitcus Mar 04 '13 at 08:06
0

You must add the redirect script in your server side code. Form must have a action url so that it will be submitted to the url by default submit click (ie, without your order() function). Inside that action url check if form data is recieved by

if(isset($_POST['order']))

and redirect the user inside that condtion as u need.

Harish Ambady
  • 12,525
  • 4
  • 29
  • 54