I am using 2 buttons to open two separate php pages but I would like to use one function to trigger both the buttons. The AJAX function that gets triggered should check which button was pressed and then open the php page associated with it. Such that "Export Page 1" should open Page 1.php and "Export Page 2" should open Page 2.php.
I am able to open one php page with my AJAX function. Now how do I check which button was pressed so I could open the right php page. How do I achieve this?
<html>
<body>
<div>
<input type ="submit" name="login" id="login" style="float: right; margin-right: 5px;" class= "btn btn-primary" value="Export Page 1" data-toggle="modal" data-target="#loginModal"/>
<input type ="submit" name="login" id="login" style="float: right; margin-right: 5px;" class= "btn btn-primary" value="Export Page 2" data-toggle="modal" data-target="#loginModal"/>
</div>
</body>
</html>
<div id="loginModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Login</h4>
</div>
<div class="modal-body">
<label>Username</label>
<input type="text" name="username" id="username" class="form-control" />
<br/>
<label>Password</label>
<input type="password" name="password" id="password" class="form-control" />
<br/>
<button type="button" name="login_button" id="login_button" class="btn btn-primary">Login</button>
<script>
$('#login_button').click(function(){
var username = $('#username').val();
var password = $('#password').val();
if(username != '' && password != '')
{
$.ajax({
url:"Login.php",
method:"POST",
data:{username:username, password:password},
success:function(data){
if(data == 'No')
{
alert("Wrong Data");
}
else
{
$('#loginModal').hide();
window.open("Page 1.php"); //For page 1
// OR window.open("Page 2.php"); //For page 2
}
}
});
}
else
{
alert("Both fields are requried");
}
});
});
</script>
3
Answers
You can use functions for that:
In the html:
Because IDs must be unique, give each button a different ID
you can then give both buttons the same event – this would also work if you gave them both the same class and did the event on that class.
Within the event, store the button id somewhere where it can be picked up later by the modal’s login button.
Because you’re auto-opening the modal, there’s a separation from open-dialog with button1/2 to click login on modal, so they’re not related. You’ll need to store on the modal/global/etc which button was used to open the modal when it’s opened so that you can use that value when you actually login.
Let’s store it on the modal’s login_button:
where this.id will be login1 or login2.
Now when the login_button is clicked, we can see which button it was:
To make this a little more usable (and less maintenance-heavy), you can make some small changes:
then
and in the callback
so when you want to add page3 etc you just add a new input with no code changes and keeps the separation of data (the page1.php) from the code (the js), which is always a goodthing(tm).
you can use the dataset aswell
make a single function for both buttons use the event parameter to extract the
data-page
out of the clicked element and pass through it