Hey guys I have this populated search form that will bring a table of people based on search result. I need to be able to get the php variable that has the username I am trying to get, then that will get sent over to .php file using the JQuery UI which will open a notes window.
So here:
username, other, other , notes - this is a link and once clicked the following is called:
$(document).ready(function () {
$("#NotesAccessor").click(function () {
alert(notes_name);
run();
});
});
which leads to :
function run(){
var url = '/pcg/popups/grabnotes.php';
showUrlInDialog(url);
}
to a function that opens the Jquery Dialog.
I need to access the username of the based notes that was clicked....if there was 20 results of different names, david, joe, john, etc... I click the notes with david as username, I need that to be sent over to the file that is opening so I can display the based notes on the appropriate username.
Here is my php:
.........
<td>
$csvusername
</td>
.........
<td>
";
if ($checkNotes[1] == 'No')
{
echo "None";
}
if ($checkNotes[1] == 'Yes')
{
echo "<a href='#' id='NotesAccessor'>Click to access</a>";
}
echo "
</td>
........
Let me know if this makes sense to you. I am kinda stuck on this and could use a hand :)
David
I can't tell what PHP variable you are trying to get, but generally, you can do:
<!-- /myScript.php?myVar=helloWorld -->
<script type="text/javascript">
$(document).ready(function(){
var myPhpVar = <?php echo $_GET['myVar']; ?>;
alert(myPhpVar);
});
</script>
<script>
var x = '<?php echo $variable; ?>';
or
var x = '<?php echo my_function(); ?>';
</script>
<?php
function my_function()
{
return $variable;
}
?>
this should do what you like :)
Related
How is possible to check if div is clicked and then return information?
function kat(){
echo "<div class='turinys'>";
$kategorijos = dbquery("SELECT * FROM kategorijos");
while($kat = dbarray($kategorijos)) {
echo"<div class='kategorija'><a href='".BASEDIR."kategorija/".seoname($kat['kategorija'])."/".$kat['id']."' class='kat'>".trimlink($kat['kategorija'],44)."</a></div>";
}
echo "</div>";
}
echo "<div class='mygtukas-js' onclick='kat();'>";
echo "</div>";
But actually it's wrong because my mygtukas-js has a drop down menu.
I need to generate a code which let me to press a button and then menu would be generated. Maybe someone knows?
EDIT: This div <div class='mygtukas-js'></div> (has to start and end before) <div class='turinys'> STARTS.
Some fresh ideas? :?
EDIT2:
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'>
</script>
<script>
$(document).ready(function(){
$('#mygtukas-js').click(function() {
$("#turinys").load('b.php');
});
});
</script>
and b.php
<?php
$kategorijos = dbquery("SELECT * FROM kategorijos");
while($kat = dbarray($kategorijos)) {
echo"<div class='kategorija'><a href='".BASEDIR."kategorija/".seoname($kat['kategorija'])."/".$kat['id']."' class='kat'>".trimlink($kat['kategorija'],44)."</a></div>";
}
?>
But no information generated :(
Create your PHP file which I guess would also include HTML.
Create a "click" JQuery function and make sure to send the POST to the specific PHP file / Controller which should handle the request. In case you want to just get the above file directly then point to it.
It should be like:
$(document).ready(function(){
$('#send').click(function() {
$("#menu").load('URL_TO_YOUR_PHP');
});
});
Thanks to AJAX the "#menu" should be displayed and will contain whatever the PHP file sends back.
HTH.
U can use jQuery to check if a div is clicked. Like this:
$('.turinys').click(function(e) {
$(this).html("Add any html to the div");
});
Use to different php file. Lets call A.php and B.php. A.php contain the menu code.
A.php
<?php
echo "<div class='turinys'>";
$kategorijos = dbquery("SELECT * FROM kategorijos");
while($kat = dbarray($kategorijos)) {
echo"<div class='kategorija'>
<a href='".BASEDIR."kategorija/".seoname($kat['kategorija'])."/".$kat['id']."' class='kat'>".trimlink($kat['kategorija'],44)."</a>
</div>";
}
echo "</div>";
?>
B.php
<script>
function kat(){
var xhr = new XMLHttpRequest();
xhr.open("GET", "A.php", false);
xhr.send(null);
// You can also check response status if you need.
var serverResponse = xhr.responseText;
document.getElementById('menu').innerHTML = serverResponse;
}
</script>
<?php
echo "<div class='mygtukas-js' onclick='kat();' id='menu'>";
echo "</div>";
?>
i give another codes for example
this is my some3.php code:(First file)
:
<head>
<script src="jquery-1.7.2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('p').click(function(){
var who = $('input#who').val();
var why = $('input#why').val();
$('#geting').load('file2.php',{who:who,why:why},function(applyData){
if ( applyData == 'YEY . Ye have hi' ){
alert('OKKK data is ok ');
} else{
alert('Nooo We dont have requested output');
}
});
});
});
</script>
</head>
<body>
<p> click </p>
<input type="text" id="who">
<br>
<input type="text" id="why">
<div id="geting" align="center">
</div>
</body>
i this my file2.php:
<?php
echo "1";
echo "2";
if($_REQUEST['who'] == "hi"){
$myVariable = "YEY . Ye have hi";
echo $myVariable;
} else{
$myVariable = "The out put is not Hi";
echo $myVariable;
}
?>
its not work why? becuse we have echo "1" and echo "2"
i want jquery just check $myVariable data not whole php callback ! i think i must use json but i dont know how
Well, assuming that you want to read the value with JQuery off the page you are posting to, you could do this, since you are echo'ing the value out in that page by doing the following: echo $myVariable;
Now this is how I generally read a value off another page with JQuery which is by using JQuery's get() method.
$.get("thepagetoretrievefrom.php", function(retrievedvalue) {
alert("Here's the data you requested: " + retrievedvalue);
if (retrievedvalue == 1) {
//print out something here
alert("The retrieved value was 1.");
}
});
And that should retrieve the value from the PHP page. "thepagetoretrievefrom.php" is the page where you want to retrieve the information from. function(retrievedvalue) just indicates that whatever output you're requesting from the page via JQuery will be put into retrievedvalue. Then, using JQuery, you may decide whether you want to do a new call to another page depending on what the "retrievedvalue" was.
This, however is not the best method to achieve this, since this will print whatever may be in that page, but if you are requesting one specific value from that page, then it shouldn't be an issue.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
passing variables from php to javascript
I'm dynamically generating a list. I want to make each row hover on mouseover and clickable to a link. I want the link to pass the id of the content of the row.
Basically:
foreach ($courses as $cid=>cinfo){
$univ = $cinfo['univ'];
$desc = $cinfo['desc'];
$size = $cinfo['size'];
$start = $cinfo['start'];
print "<div class='rc_desc' id='rc_desc$cid'>"."$desc<br/>"."<b>$univ</b><br/>".
"<span>Number of students</span>: $size<br/>".
"<span>Started at</span>: ".date('F d, Y',strtotime($start))."<br/>".
}
<script>
$(function ()
{
$('#rc_desc$cid').hover(function ()
{
$(this).toggleClass('.tr');
});
$('#rc_desc$cid').click(function ()
{
$(location).attr('href','student.php?$cid');
});
});
</script>
The issue is in the js/jquery. I want to be able to grab the $cid and pass it to the student.php page upon click. The php code above works but the js won't of course. I know the fundamental of client-side vs server-side languages. This question doesn't warrant a lecture. I know I cannot do this exactly, but it is what I want to happen ultimately. Any thoughts on how I can achieve this simply? Thanks in advance my friends!
Yes, if you include the <script> section in your PHP code, you can do something similar to the following:
<script>
var foo = <?php echo $foo; ?>;
</script>
In your case, you would be looking into the following code structure:
<script>
$(function () {
$('#rc_desc<?php echo $cid ?>').hover(function () {
$(this).toggleClass('.tr');
});
$('#rc_desc<?php echo $cid ?>').click(function () {
$(location).attr('href', 'student.php?<?php echo $cid ?>');
});
});
</script>
The reason why this is possible is because although the Javascript is run on the client-side, it's processed on the server-side first prior to being presented on the page. Thus, it'll replace all necessary instances of $cid with the one you have included.
Enjoy and good luck!
EDIT:
<script>
$(function () {
$('.rc_desc').hover(function () {
$(this).toggleClass('.tr ');
});
$('.rc_desc').click(function () {
$(location).attr('href', 'student.php?' + $(this).attr('id').split('rc_desc')[1]);
});
});
</script>
You can do it like this :
$(location).attr('href','<?php echo $cid ?>');
The javascript code doesn't know it comes from php, it appears as a constant (a literal) for it.
Yes it is possible. All you have to do is put your <?PHP echo $cid; ?> in where you need it
<script>
$(function ()
{
$('#rc_desc<?PHP echo $cid; ?>').hover(function ()
{
$(this).toggleClass('.tr');
});
$('#rc_desc$cid').click(function ()
{
$(location).attr('href','student.php?<?PHP echo $cid; ?>');
});
});
This is possible because by the time the scrip is put into the page the cid has already been replaced by the string on the server. Since PHP is server driven, before it spits back the html/script it will be just like you put it in yourself.
There is almost no way to actually communicate PHP and JavaScript. However, the best and simplest way is to set the ID within an attribute. The new HTML5 data attributes would be perfect.
For example, have a anchor tag with
<span data-id="22">some event</a>
and then:
$(this).attr('href','student.php?'+$(this).attr('data-id'));
Or just use
<?php echo $id; ?>
if it is not external JS file
Try this code
foreach ($courses as $cid=>cinfo){
$univ = $cinfo['univ'];
$desc = $cinfo['desc'];
$size = $cinfo['size'];
$start = $cinfo['start'];
print "<div class='rc_desc' data-id='$cid' id='rc_desc$cid'>"."$desc<br/>"."<b>$univ</b><br/>".
"<span>Number of students</span>: $size<br/>".
"<span>Started at</span>: ".date('F d, Y',strtotime($start))."<br/>".
}
<script>
$(function ()
{
$('#rc_desc$cid').hover(function ()
{
$(this).toggleClass('.tr');
});
$('.rc_desc').click(function ()
{
$(location).attr('href','student.php?' + $(this).attr("data-id"));
// if you want to redirect the page do this
// window.location.href = 'student.php?' + $(this).attr("data-id");
});
});
</script>
I think you block should be :
<script>
$(function ()
{
<?php foreach($courses as $cid=>cinfo) : ?>
$('#rc_desc<?php echo $cid; ?>').hover(function ()
{
$(this).toggleClass('.tr');
});
$('#rc_desc<?php echo $cid; ?>').click(function ()
{
$(location).attr('href','student.php?<?php echo $cid; ?>');
});
";?>
<?php endforeach; ?>
});
</script>
UPDATE
But you don't really need to do this, you have
"<div class='rc_desc' data-id='$cid' id='rc_desc$cid'>"."$desc<br/>"."<b>$univ</b><br/>".
"<span>Number of students</span>: $size<br/>".
"<span>Started at</span>: ".date('F d, Y',strtotime($start))."<br/>".
You can try this
$(function (){
$('.rc_desc').hover(function ()
{
$(this).toggleClass('.tr');
});
$('.rc_desc').click(function ()
{
attrId = $(this).attr("data-id");
$(location).attr('href','student.php?'+id);
});
});
i have a problem when it comes to retrieving value from jQuery to php.i was able to get the value of my select and pass it to my php but i can't pass it back to php.
here is the code...
<script>
$(document).ready(function()
{
$("select#months").change(function(event)
{
var m=$(this).val();
alert(m);
});
});
</script>
<div>
<select id="months">
<option value='00'>Month...</option>
<option value='01'>Jan</option>
<option value='02'>Feb</option>
<option value='03'>Mar</option>
<option value='04'>Apr</option>
</select>
<select id="years">
<?php
for($yr=10; $yr<=$year; $yr++)
{
echo "<option value='".$yr."'>".$years[$yr]."</option>";
}
?>
</select>
</div>
now i have to get the m variable from the jQuery code and echo it on my php.
You need to post or get the page again, passing "m" as a parameter and reading it inside php.
Php and jquery can communicate with each other only using http posts/gets, cause they run on two different computers (php on your server, jquery on your users browser)
Unfortunately, to answer this question completely would mean teaching client-server from the basics.
The jQuery code will run on your user's browser. You need to send m via POST (or GET) to your PHP script upon change.
Try this:
$(document).ready(function()
{
$("select#months").change(function(event)
{
var m=$(this).val();
$.ajax({
type: 'POST',
url: "http://example.lan/your.php",
data: {m: m},
success: function(){alert("updated")}
});
});
});
and on your PHP code, do something about m if it is set:
<?php
if (isset($_POST['m'])) {
// do stuff with M here
}
?>
PHP and Javascript may appear in the same file, but they are far from being executed by the same machine. For example, take this simple script :
<?php
$foo = "Hello world!";
?>
<div id="mydiv"><?php echo $foo; ?></div>
<script type="text/javascript">
$(function() { alert( $('#mydiv').text() ); });
</script>
When you access this script from your browser, the server actually only parse this :
<?php
$foo = "Hello world!";
echo $foo;
Because everything else is not PHP, thus not processed by the engine (skipped at parsing time). Therefore, the document that is uploaded by the server to your browser is
<div id="mydiv">Hello world!</div>
<script type="text/javascript">
$(function() { alert( $('#mydiv').text() ); });
</script>
As you see, at this point, there is no PHP at all in what's outputted by the server (what is received by the client), so there is no way that you can reference anything PHP at that point. The only way you can do this is by calling the same script again! (or any other PHP script)
PHP may receive parameters from external sources via $_GET and $_POST. Thus, modifying our code would be like
<?php
if (isset($_GET['foo'])) {
$foo = $_GET['foo']; // we received our param like
// '?foo=Hi!` from the HTTP request
} else {
$foo = "Hello world";
}
?>
<div id="mydiv"><?php echo $foo; ?></div>
<form action="" method="get">
<input type="text" name="foo" value="" />
<input type="submit" name="submit" />
</form>
<script type="text/javascript">
$(function() { alert( $('#mydiv').text() ); });
</script>
And you can communication in both directions. For XHR with jQuery, there is no need to send a whole bunch of HTML with the query, we need to modify the source a little:
<?php
if (isset($_GET['foo'])) {
echo "Message got : " . $_GET['foo'];
exit; // do not execute any more code from now
} else {
$foo = "Hello world";
}
?>
<div id="mydiv"><?php echo $foo; ?></div>
<script type="text/javascript">
$(function() {
alert( $('#mydiv').text() );
// note : assuming the code is in file "example.php"
$.get('example.php', {foo: "Hi!"}, function(data) {
alert(data); // will show "Response got : Hi!";
});
});
</script>
In the last example, the file is parsed and processed twice by the server, the code is actually executed in this order :
the browser makes a first query to the PHP script
the server parse the file and sends the HTML,
the client receives the HTML
the Javascript code is executed
alert : "Hello world!"
create and send a GET XHR with parameter foo=Hi!
the server receives another request and parse the file
the $_GET['foo'] argument is found
echo "Response got : Hi!"
the client receives the XHR response and invoke the callback function
alert : "Response got : Hi!"
Now, this last example may seem a bit more complicated, but really as simple as the other code :
<?php
$dataFile = 'data.txt';
if (isset($_POST['text'])) {
file_put_contents($dataFile, $_POST['text'];
echo "Saved!";
exit;
} else if (file_exists($dataFile)) {
$text = file_get_contents($dataFile);
} else {
$text = '';
}
?>
<a id="lnkSave" href="example.php">Save</a>
<script type="text/javascript">
var text = '<?php echo addslashes($text); ?>';
$(function() {
$('<textarea></textarea>').attr('id', 'text').appendTo(document).val(text);
$('#lnkSave').click(function() {
$.post($(this).attr('href'), {text:$('#text').val()}, function(data) {
alert(data);
});
});
});
</script>
I'll let you figure this one out; what it does and how it works.
I am have a table that shows the user suggestions that they have recieved on clicking read more some ajax is fired and in the database the suggestion is marked as read. Currently if the suggestion is new I show a closed envelope, if it is read I show an open envelope, however I can get it to reload the table when the user clicks the read more link so that the new class can be added. Currently it half works, they click read more and the full suggestions fades in but I need the envelope to change also.
<table>
<?php
$colours = array("#f9f9f9", "#f3f3f3"); $count = 0;
if(isset($newSuggestions)) {
foreach($newSuggestions as $row) {
if($row['commentRead'] == 0) {
$newRow = "new";
} else {
$newRow = "old";
}
?>
<tr id="a<?=$row['thoughtId'];?>" bgcolor="<?php echo $colours[$count++ % count($colours)];?>">
<?php
echo "<td class='".$newRow."'>".substr($row['thought'], 0,50)."...</td>";
echo "<td class='read'><a href='".base_url()."thought/readSuggestion/".$row['thoughtId']."' class='readMore'>Read More</a>";
echo "</tr>";
}
} else {
echo "You have no new suggestions";
}
?>
</table>
</div><!--/popular-->
</div><!--/widget-->
<div id="readMore">
</div>
<script type="text/javascript">
$(document).ready(function() {
//alert("hello");
$('#tabvanilla').tabs({ fx: { opacity: 'toggle', height:'toggle' } });
$('a.readMore').click(function(){
$('#readMore').fadeIn(500);
var url = $(this).attr('href');
$.ajax({
url : url,
type : "POST",
success : function(html) {
$('#readMore').html(html)
},
complete : function(html) {
$('table').html()
}
});
return false;
});
});
</script>
In the JavaScript where you open/fill in the full suggestions, you can modify the envelope image as well, using something like:
$('#envelope').attr('src', 'src/to/envelope.png');
I see no img tags, so you need to add one and fill in the id, so it is found by the JavaScript.
BTW: Having HTML and PHP on the same lines/parts, makes the total very unreadable. Only use <?php ... ?> for large PHP code blocks, otherwise use echo (or something similar).