4
$(document).ready(function() {
  $("#submit").click(function() {
    var value = document.getElementById("txt_sketch_no").value;
    var process_id = document.getElementById("process_id").value;
    var length = value.length;
    if (length <= 0) {
      alert("Please Enter the sketch card");
      window.location = 'sketch_screen.php';
    } else {
      window.location = 'dws_Support.php';
    }
  });
});
<form action="" method="post" autocomplete="off">
  <div class="container">
    <div class="alert alert-success" id="alert_box">
      <input type="hidden" name="process_id" id="process_id" value="<?php echo $process_id; ?>">
      <label>Enter the Sketch Card No :</label>
      <input type=text id="txt_sketch_no" name="txt_sketch_no">
      <input type=submit id="submit">

In the above code I would like to redirect the page with the help of Javascript, but window.location = 'dws_Support.php' in else part is not working, but when we alert with in the else part it gets displayed but not redirected to 'dws_Support.php'. Please help.

Mr Lister
  • 44,061
  • 15
  • 107
  • 146
user1894647
  • 553
  • 3
  • 9
  • 22
  • 2
    Use window.location.href or window.location.assign – Sunil Pachlangia Sep 28 '15 at 06:28
  • my guess is that you're submitting the form prior to validation with jquery. Try validate it first. http://stackoverflow.com/a/15072147/797495 – Pedro Lobito Sep 28 '15 at 06:32
  • Are you sure the redirect in the `if` part works? – t.niese Sep 28 '15 at 06:37
  • is this working? `if (length <= 0) { alert("Please Enter the sketch card"); window.location = 'sketch_screen.php'; return; } else { window.location = 'dws_Support.php'; }` – Ari Sep 28 '15 at 06:44
  • @MrLister, I used the tidy button from the snippet function. Didn't noticed it created an error. Obviously I didn't do that on purpose... – LinkinTED Sep 28 '15 at 07:10
  • @LinkinTED Oh dear. That's a baddie. Sorry to have accused you! I just posted a bug report on that one, [here](http://meta.stackoverflow.com/q/306941/1016716). – Mr Lister Sep 28 '15 at 07:25
  • @MrLister, no problem. :) – LinkinTED Sep 28 '15 at 08:02

4 Answers4

2

Try using window.location.href='dws_Support.php'

Gautam
  • 1,046
  • 5
  • 19
0

you don't need form for redirecting with javascript ,problem come from your attribute action form, so use this code:

<script src="js/jquery.js"></script>
<script>
    $(document).ready(function() {
        $("#submit").click(function() {
            var value = document.getElementById("txt_sketch_no").value;
            var process_id = document.getElementById("process_id").value;
            var length = value.length;
            if (length <= 0) {
                alert("Please Enter the sketch card");
                window.location = 'sketch_screen.php';
            } else {
                window.location = 'dws_Support.php';
            }
        });
    }); 
</script>
</head>
<body style="background-color: #73C5E1" >
        <div class="container">
            <div class="alert alert-success" id="alert_box">
                <input type="hidden" name="process_id" id="process_id" value="12">
                <label> Enter the Sketch Card No : </label>
                <input type="text" id="txt_sketch_no" name="txt_sketch_no">
                <input type = "submit"  id="submit">
            </div>
        </div>
</body>

hope it help you.

0

Here is your fail: you are listening click, but submit event of the form brakes your logic. This is the fixed one:

$(document).ready(function() {
  $("form").on('submit', function(e) {
    e.preventDefault();

    var value = document.getElementById("txt_sketch_no").value;
    var process_id = document.getElementById("process_id").value;
    var length = value.length;
    if (length <= 0) {
      alert("Please Enter the sketch card");
      window.location = 'sketch_screen.php';
    } else {
      window.location = 'dws_Support.php';
    }
  });
});
MysterX
  • 2,258
  • 1
  • 10
  • 11
0

glad if you found the answer, but there is no problem with your coding, you just missed the return false; after redirect..

<script src="jquery-1.4.js"></script>
<script type="text/javascript">
<!--
    $(document).ready(function() {
      $("#submit").click(function() {
        var value = document.getElementById("txt_sketch_no").value;
        var process_id = document.getElementById("process_id").value;
        var length = value.length;
        if (length <= 0) {
          alert("Please Enter the sketch card");
          window.location = '/sketch_screen.php';
          return false;
        } else {
          window.location = 'dws_Support.php';
          return false;
        }
  });
});
//-->
</script>

hope this can fix your problem. :)

Willyanto Halim
  • 403
  • 6
  • 19