Reputation: 663
$(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.
Upvotes: 4
Views: 2228
Reputation: 413
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. :)
Upvotes: 0
Reputation: 2368
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';
}
});
});
Upvotes: 0
Reputation:
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.
Upvotes: 0