I am trying to pass a PHP GET variable to AJAX so the appropriate content according to the ID passed can be loaded on the same page but I don't know how to do it exactly. The ID is dynamically generated by a loop and the ID is given in the image hyperlinks below. So basically when they click on an image containing the link and the id that it is passed on, new content will load on the same page.
Below is the code.
<div id="myDiv">
<h2>Let AJAX change this text</h2>
You want to use the global $_GET variable.
Example:
www.some-url.com/script.php?id=1337
and in the script to access it:
echo $_GET['id']
> 1337
try to put parameter in the function you called...
onclick='loadXMLDoc(".$row['ID'].")'
or you can put the $row['ID'] in the input and set as hidden, then retrieve the value using ajax.
<input type="hidden" value="<?php echo $row['ID']; ?>" id="someid" />
retrieve
var ID = $('#someid').val();
Related
is it possible to somehow send $_POST[] data via a <a> tag? because it seems to automaticly execute the $_POST[] once the page loads rather than when I press the Anchor tag.
edit:
so what I'm trying to achieve is something like this:
I want an anchor tag which would normaly go to somepage.php?variable=something
but instead of using $_GET[] I want to send the variable via $_POST
Nothing in HTML will cause a link to trigger a POST request or encode data in the request body.
You can bind a JavaScript event handler to a link, cancel the default behaviour and then send a POST request by programmatically submitting a form or using XMLHttpRequest. This generally isn't a good idea and you should use a submit button instead (which you can style to look like a link if you really, really want to).
You can achieve this using jQuery and a HTML form
HTML:
<form method="post" name="redirect" class="redirect">
<input type="hidden" class="post" name="post" value="">
<input type="submit" style="display: none;">
</form>
Button: (html)
<a href='javascript:void(0)' class='button' var='DATAHERE'>sometexthere</a>
Javascript, or rather said jQuery:
$(".button").click(function() {
var link = $(this).attr('var');
$('.post').attr("value",link);
$('.redirect').submit();
});
this jQuery code listen's to any clicks on the items with the class button attached to them,
and reads out their "var" value, basicly you could use any kind of HTML element using this method as long as they have the button class attached to it.
Answer is no. What you can do is set the value of an input type when the <a> tag gets clicked. Using Javascript.
You could get hold of the query string from the href attribute of the anchor tag (you would need to parse the href and get hold of the string on the right hand side of the & symbol) and then post it using javascript or jquery (easier to use jquery).
http://api.jquery.com/jquery.post/
Would have to ask why you would want/need to do this?
I am stuck in a problem and in need of support from you guys.
My problem is I want to pass a php variable(dynamically called through database in loop) to another div on the same page without page refresh
//This is a loop
<td><a class="linkright" href="#existingcase?case_id=<?php echo $row_mycases['case_id']; ?>"><?php echo $row_mycases['case_id']; ?></a></td>
//The div which should get the above php variable
<div class="tabright" id="existingcase">
<?php
$c_id = $_GET['case_id'];
echo $c_id;
?>
</div>
//the javascript code for calling divs on the same page
<script>
$(".linkright").click(function(){
$(".tabright").hide();
theDiv = $(this).attr("href");
$(theDiv).slideToggle();
});
</script>
It shows in the url like this index.php#existingcase?case_id=2012001 but never passes the case id to #existingcase. And also #existingcase div does not load either but without passing caseid value, #existingcase loads.
Any suggestions would be great.
I think you want to print clicked case_id in div without page load. To do this, you don't want to pass it using url. you can simply achieve it using javascript.
If your div id existingcase is just change your code like this below.
you are printing that same case_id as a text of anchor tag using $row_mycases['case_id'];. So, you can easily get that text using javascript and you can easily print it in your #existingcasediv.
<td><a class="linkright" href="#existingcase"><?php echo $row_mycases['case_id']; ?></a></td>
I don't know about your other scripts. in .linkright click function place this code like this
$(".linkright").click(function(){
$('#existingcase').text($(this).text()); //if your div id is existingcase it will print your case id.
});
try this code. And let me know the result.
SEE THIS FIDDLE DEMO
UPDATED:
To pass client side value to serverside without page reload, you can use jquery.post() method.
Place this PHP code at the top of your page.
<?php
if (isset($_POST['id'])) {
$caseid = $_POST['id'];
return print_r($caseid);
}
?>
$caseid will contain currently clicked case_id value. So, you can use this $caseid wherever you want in this page. If you click on the another case id, it will update with respect to currently clicked case_id
replace your js with below code,
$(".linkright").click(function () {
$(".tabright").hide();
theDiv = $(this).attr("href");
$(theDiv).slideToggle();
$.post("yourPHPFile.php", { //use your current php file name instead of "yourPHPFile.php"
id: $(this).text()
}, function (caseid) {
$('#existingcase').text(caseid);
});
});
id : $(this).text() means, get the current element .text() and assign it to $_POST variable name of id. It will post to yourPHPFile.php. And you can retrieve that value like $caseid = $_POST['id'];.
In function (caseid) {, caseid contains the value of $caseid. So, only in this code, I assigned $caseid = $_POST['id'];
By this you can directly print clicked case_id text to your #exixtingcase div.
Any values after # are not sent to the server. Since PHP is a server technology you php code will never see the values after #
<a class="linkright" href="#existingcase?case_id=<?php echo $row_mycases['case_id']; ?>">
try doing
<a class="linkright" href="existingcase?case_id=<?php echo $row_mycases['case_id']; ?>">
instead and your php code should fill the div.
In my understanding of your explanation I think you are making a mistake. You are trying to mix Javascript and PHP with out page refresh. This is not possible through your code as PHP is server side and only works on the server. You can't do href within the same page and expect PHP code inside this div to be executed. The PHP code will not be there. You can try "view source" in your browser and check it out.
I think the only way to solve your problem is to make AJAX call using Javascript, get the response and put wherever you want (without refreshing the page)
Hope this helps and excuse my English
EDIT : Here is a working example for making a simple AJAX call using Jquery.
Your table should be some thing like this:
<td>
<a class="linkright" href="#" data-id="<?php echo $row_mycases['case_id']; ?>"><?php echo $row_mycases['case_id']; ?></a>
</td>
You will notice I added "data-id" attribute. We will use this id in our AJAX call. Put this in your JS :
// Create Jquery ajax request
$("a.linkright").click( function() {
$.get("example.php", // Put your php page here
{ case_id : $(this).attr("data-id") }, // Get data, taken from the a tag
function(data) { // Call back function displays the response
$(".tabright").html("<p>" + data + "</p>");
}); // Get
}); // click event
}); // Document Ready
I tested the code and it is working. You will need to customize it to work for you. If all of this AJAX is new to you I suggest these links for you :
http://www.w3schools.com/ajax/default.ASP
http://learn.jquery.com/ajax/
On my website, I have a button which when clicked displays a feedback form in a popup window.
I need to pass the url in the PARENT window to a hidden field in the feedback form.
I tried:
<?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>
<input type="hidden" id="addressBar" name="addressBar" value= "<?php echo $url ?>"/>
The problem with the above code is that it passes the url of the current window, in my case, the url of the feedback popup window.
How do I pass the url of the parent window to that hidden field?
Many thanks for your help
You can try $_SERVER['HTTP_REFERER'] instead of REQUEST_URI (it gives you current uri)
but keep in mind that it's not reliable...
$url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'some_default_page.php';
Another way could be something like this ...
$cur_page_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo 'Feedback';
and then in popup.php just check if ref query string is set and retrieve its value.
I have a variable called $tags, which just references from a database field. When the user clicks a link with the given $tags output in it, I want that data to be stored in a variable on the target page.
For instance, the user is on a todolist.php, that contains several tasks. They can see the parts associated with this task by clicking a link that goes to a partslist.php page. In the link, I need to contains the $tags data, so javascript on the partslist.php page knows what part to highlight.
So I need to know 1) how do I output $tags in the link of todolist.php, and 2) how do I receive that output and store it on a variable on the partslist.php page?
I have done similar POST and GET commands, but I can't quite figure this out.
partslist.php?tags=<?php echo urlencode($tags) ?> .. is this what you are asking ? Or there are multiple tags ? Please give examples / code blocks / links
On the partslists side you can do this :
$tags = $_GET['tags']; // You don't need urldecode here, because $_GET is a supergobal and it is already decoded.
You pass URLs to pages in one of two ways: $_GET or $_POST. $_POST requires that you have a form setup like:
<form method="post" action="page.php">
<input type="text" name="myvariable" value="test">
<input type="submit">
</form>
And $_GET requires that you link to the page like:
click me
In the first scenario, when the user hits submit, page.php will have access to the variables submitted from the $_POST array. In this case, $_POST['myvariable'] will equal "test";
In the second, $_GET['myvariable'] will equal test.
is $tags an array?
If so you can output the $tags array in the following format
partslist.php?tags[]=tag1&tags[]=tag2&tags[]=tag3
if you would then output the $_GET global in the partslist.php script you would end up with an array like this:
Array
(
[tags] => Array
(
[0] => "tag1"
[1] => "tag2"
[2] => "tag3"
)
)
If the URL for the link they click on contains URL parameters, those will be available on the target page. For instance,
// Generate a link like this with PHP
<a href="somepage.php?name=bob&weight=150">
// On somepage.php
$name = $_GET['name'];
$weight = $_GET['weight'];
Note: for security, you may want to encrypt this data before putting it in the link, and decrypt it on the other end.
For the part where you hand some data to Javascript, you can interpolate PHP in your Javascript the same as your HTML. So, you could say:
//JS
var foo = <?PHP echo $foo ?>;
Alternately, you could store data in an HTML element:
<div id="foo" data="<?PHP echo $data ?>">
... and then grab that using Javascript.
if i have a text field like this:
<input type="text" name="quantity" id="txtquantity">
what i am trying to figure out is how can i grab the value of that text field and append it to a link like this:
index.php?quantity=(the value)
if this is done with jquery how would i do it?
Im not sure if you are trying to do this on the page the input gets posted to, or the current page. If you are trying to do it on the page the form gets posted to you would need to use PHP like the following.
<?php
echo '<a href="'.$_GET['txtquantity'].'" />';
?>
However, if you are trying to do it before the form is submitted then using jQuery you would do it like this,
$("a").attr("href", $(this).attr("href") + $("#txtquantity").val());
You can do like:
<a id="your_link_id" href="whatever">A Link</a>
jQuery:
$('#your_link_id').attr('href', $(this).attr('href') + $('#txtquantity').val(););