<script type="text/javascript">
$(document).on("click", ".open-AddBookDialog", function (e) {
e.preventDefault();
var _self = $(this);
var myBookId = _self.data('id');
$("#bookId").val(myBookId);
$(_self.attr('href')).modal('show');
});
</script>
<a data-id="<?php echo $id_family_member;?>" title="Add this item" class="open-AddBookDialog btn btn-primary" href="#addBookDialog">test</a>
<input type="text" name="bookId" id="bookId" value="" />
Any ideas on how to get a button or a link to pass the variable to a PHP page?Can you explain?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CE National Event Registration System</title>
<link rel="stylesheet" href="../support/bootstrap.css">
<link href="../support/style.css" rel="stylesheet" type="text/css" />
<script src="../support/jquery.min.js"></script>
<script src="../support/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).on("click", ".open-id_family_member", function (e) {
e.preventDefault();
var _self = $(this);
var myBookId = _self.data('id');
$("#bookId").val(myBookId);
$(_self.attr('href')).modal('show');
});
</script>
</head>
<body>
<p>Choose a family member and the modal pops up with a list of events that the family member can register for.</p>
<p align="center"><a data-id="Family Member 1" title="Add this item" class="open-id_family_member btn btn-primary" href="#addEventDialog">Family Member 1</a></p>
<p align="center"><a data-id="Family Member 2" title="Add this item" class="open-id_family_member btn btn-primary" href="#addEventDialog">Family Member 2</a></p>
<form action="test2.php" method="post" name="form1" id="form1">
<!-- Modal -->
<form name="form1" method="post" action="get_event_requirements.php">
<div class="modal fade" id="addEventDialog" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Choose Your Event</h4>
</div>
<!--modal-header-->
<div class="modal-body">
<a href="test2.php?id_family_member=<?php echo $id_family_member;?>"> Event 1</a> (Since multiple events, I want these links to pass the id_family_member to the next page (test2.php) instead of using mutiple buttons)<br />
<a href="test2.php?id_family_member=<?php echo $id_family_member;?>"> Event 2</a> <br />
<a href="test2.php?id_family_member=<?php echo $id_family_member;?>"> Event 3</a> <br />
<input type="text" name="bookId" id="bookId" value="" />
<input name="pass_Id" type="submit" value="Pass Id" />
</form>
</div><!--modal-body-->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div><!--modal-footer-->
</div><!--modal-content-->
</div><!--modal-dialog-->
</body>
</html>
// Representative of an event where ID is passed
$('#passId').click(function() {
// get the id you want to pass
var id = $(this).val();
// Loop through the event links appending the id to the URL
$('.modal-body a').each(function() {
// Get the current url and append the id
var href = $(this).attr('href') + '&id=' + id;
// Save the URL back
$(this).attr('href', href);
});
});
<script type="text/javascript">
$('#passId').click(function() {
console.log();
// get the id you want to pass
var id_family_member = $(this).val();
// Loop through the event links appending the id to the URL
$('.modal-body a').each(function() {
// Get the current url and append the id
var href = $(this).attr('href') + '&id_family_member=' + id_family_member;
// Save the URL back
$(this).attr('href', href);
});
});
</script>
<p align="center"><a data-id="Family Member 1" title="Add this item" class="open-id_family_member btn btn-primary" href="#addEventDialog" id="passId">Family Member 1</a>
$(function() {
$('#passId').click(function() {
console.log();
// get the id you want to pass
var id_family_member = $(this).val();
// Loop through the event links appending the id to the URL
$('.modal-body a').each(function() {
// Get the current url and append the id
var href = $(this).attr('href') + '&id_family_member=' + id_family_member;
// Save the URL back
$(this).attr('href', href);
});
});
});
We need to make sure we are getting the right value to pass in. That is what this line does // get the id you want to pass
var id_family_member = $(this).val();
Except this line is assuming the value is stored in an <input> somewhere on the page. <input name="id_family_member" id="id_family_member" type="text" value="smith123456" />
That is fine - now we nee to update our jQuery to find this control. The current implement uses $(this) which is going to refer to the element that generated the click event (the <a>) which is not what we want. We need to make a simple change to get the value from the input above$(function() {
$('#passId').click(function() {
console.log();
// UPDATE: we are now targeting the input with id = id_family_member
var id_family_member = $('#id_family_member').val();
// Loop through the event links appending the id to the URL
$('.modal-body a').each(function() {
// Get the current url and append the id
var href = $(this).attr('href') + '&id_family_member=' + id_family_member;
// Save the URL back
$(this).attr('href', href);
});
});
});
<input name="id_family_member" id="id_family_member" type="text" value="<?php echo $id_family_member;?>" />
<a data-id="<?php echo $id_family_member;?>" title="Get event" class="open-id_family_member btn btn-primary" id="passId" href="#addBookDialog">Register</a>
<td><a href="fm_view_family_member.php?id_family_member=<?php echo $id_family_member;?>"><?php echo $row_f_members['fm_lname'];?></a></td>
<td><?php echo $row_f_members['fm_fname'];?></td>
<td><div class="container">
<!-- Trigger the modal with a button -->
<input name="id_family_member" id="id_family_member" type="text" value="<?php echo $id_family_member;?>" />
<a data-id="<?php echo $id_family_member;?>" title="Get event" class="open-id_family_member btn btn-primary" id="passId" href="#id_family_dialog">Register</a>
<!-- Modal -->
<form name="form1" method="post" action="get_event_requirements.php">
<div class="modal fade" id="id_family_dialog" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Choose Your Event</h4>
</div> <!--modal-header-->
<div class="modal-body">
<input type="text" name="bookId" id="bookId" value="<?php echo $id_family_member;?>" />
<input name="pass_ISBN" type="submit" value="<?php echo $id_family_member;?>" />
Events:<br />
<?php
foreach ($array_events as $sub_array) {
$id_event_group=$sub_array['id_event_group'];
$id_event = $sub_array['id_event'];
$event_name = $sub_array['event_name'];
$event_group_desc = $sub_array['event_group_desc'];
echo $event_group_desc?>
-- <a href="get_event_requirements.php?id_event=<?php echo $id_event;?>&id_family_member=<?php echo $id_family_member;?>&id_event_group=<?php echo $id_event_group;?>"><?php echo $event_name;?><br /></a>
<?php } ?>
<?php
foreach ($array_events_no_lc as $sub_array) {
$id_event_group=$sub_array['id_event_group'];
$id_event = $sub_array['id_event'];
$event_name = $sub_array['event_name'];
$event_group_desc = $sub_array['event_group_desc'];
echo $event_group_desc?>
-- <a href="get_event_requirements.php?id_event=<?php echo $id_event;?>&id_family_member=<?php echo $id_family_member;?>&id_event_group=<?php echo $id_event_group;?>"><?php echo $event_name;?></a><br />
<?php } ?>
</div><!--modal-body-->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div><!--modal-footer-->
</div><!--modal-content-->
</div><!--modal-dialog-->
</div><!--modal-fade-->
</div><!--End of container-->
</td>
$(function() {
$('#passId').click(function() {
console.log();
// UPDATE: family_id is stored in the links data-id so we get it from there
var id_family_member = $(this).datal('id');
// Loop through the event links appending the id to the URL
$('.modal-body a').each(function() {
// Get the current url and append the id
var href = $(this).attr('href') + '&id_family_member=' + id_family_member;
// Save the URL back
$(this).attr('href', href);
});
});
});
Why is the '1' there?Because I type and don't check - should be data('id')
var href = $(this).attr('href') + '&id_family_members=' + id_family_member;
Which raises the question - is this process adding a single family_id to the link or are we trying or add more than one?
foreach ($array_events as $sub_array) {
$id_event_group=$sub_array['id_event_group'];
$id_event = $sub_array['id_event'];
$event_name = $sub_array['event_name'];
$event_group_desc = $sub_array['event_group_desc'];
echo $event_group_desc?>
-- <a href="get_event_requirements.php?id_event=<?php echo $id_event;?>&id_event_group=<?php echo $id_event_group;?>"><?php echo $event_name;?><br /></a>
<?php } ?>
<?php
foreach ($array_events_no_lc as $sub_array)
id_event_group=$sub_array['id_event_group'];
$id_event = $sub_array['id_event'];
$event_name = $sub_array['event_name'];
$event_group_desc = $sub_array['event_group_desc'];
echo $event_group_desc?>
-- <a href="get_event_requirements.php?id_event=<?php echo $id_event;?>&id_event_group=<?php echo $id_event_group;?>"><?php echo $event_name;?></a><br />
I should have mentioned that I tried data('id') and it didn't work.Can you be more specific - was there an error or did it just do what you expected.
$(function() {
// Change selector to target the class of the button
$('.open-id_family_member').click(function() {
// UPDATE: family_id is stored in the links data-id so we get it from there
var id_family_member = $(this).data('id');
// Loop through the event links appending the id to the URL
$('.modal-body a').each(function() {
// Get the current url and append the id
var href = $(this).attr('href') + '&id_family_member=' + id_family_member;
// Save the URL back
$(this).attr('href', href);
});
});
});
In you current implementation you will see the first button is actually working - the value is being appended.the other buttons don't have that ID so they won't trigger the event.Correction - I see they do - remove the id="passId" from the buttons - id's must be unique and in this case you don't need the id anymore.
When it is a form submit button, then store your value in a hidden form field.
When it is just a generic button build on an anchor append it as a parameter.
But I don't see how your code calls another page. It looks more like bootstrap. In this case you don't need to pass anything. Cause your "model window" is part of your current page. Thus your variable is already in scope.