Javascript Function Accepting PHP Variables - php

I'm drawing a complete blank why this isn't working. I can do it with one variable passing through, but not two. When I use actually numbers like getnt(1,2) it works. It's just not working with two PHP variables.
<script type="text/javascript">
function getnt(nid,pnid) {
window.location = "nt.php?nid=" + nid + "&pnid=" + pnid;
}
</script>
<body>
<?php
echo "<a href='#' onclick='getnt($nid,$pnid)'>VIEW</a>";
?>
</body>
I can make the code work with echo "<a href='nt.php?nid=$nid&pnid=$pnid'>VIEW</a>";, but that's no good if I want to add in alerts and javascript commands.

If the ID and pnID are strings, enclose them with brackets like this.
<body>
<?php
echo "VIEW";
?>
</body>
If still not working, You can debug your code
View the source code in browser,
make sure it generates correctly.
Put some alert messages in the
javascript function. Install Firebug
if you have Firefox or see
Javaascript console if you get any javascript errors.

You could always try:
<script type="text/javascript">
function getnt(nid,pnid) {
window.location = "nt.php?nid=" + nid + "&pnid=" + pnid;
}
</script>
<body>
VIEW
</body>

Your question is probably best answered by looking at the rendered HTML source.
In any case, here's how I'd do it using graceful degradation
<script type="text/javascript">
function getnt(element) {
var href = element.href;
var nid = element.getAttribute("data-nid");
var pnid = element.getAttribute("data-pnid");
return true;
}
</script>
<p><a href="nt.php?nid=<?php echo $nid ?>&pnid=<?php echo $pnid ?>"
data-nid="<?php echo $nid ?>"
data-pnid="<?php echo $pnid ?>"
onclick="return getnt(this)">VIEW</a></p>

Related

jquery and strings from php code

I have a problem how to pass for example $example=1; variable to this jquery like a value in bracket where is 20, 40, 60.....:
<script type="text/javascript">
$(document).ready(function() {
$("#pb1").progressBar(20);
$("#pb2").progressBar(40);
$("#pb3").progressBar(60);
$("#pb4").progressBar(70);
$("#pb5").progressBar(100);
$("#pb6").progressBar(100);
});
</script>
The main problem is that I want to example string have value from database, so that's why I need to make it outside of script.
If this code is in your .php page...
<script type="text/javascript">
$(document).ready(function() {
$("#pb1").progressBar(<?php echo $pb1 ;?>);
$("#pb2").progressBar(<?php echo $pb2 ;?>);
$("#pb3").progressBar(<?php echo $pb3 ;?>);
$("#pb4").progressBar(<?php echo $pb4 ;?>);
$("#pb5").progressBar(<?php echo $pb5 ;?>);
$("#pb6").progressBar(<?php echo $pb6 ;?>);
});
</script>
Where $pb1,$pb2,$pb3,$pb4,$pb5,$pb6 are Integers ..
This is simple:
<?php
//Insert your code to generate the values to $example
$example = ...
?>
// and here is your JS with php code inside it
<script type="text/javascript">
$(document).ready(function() {
$("#pb1").progressBar(<?php echo $example; ?>);
$("#pb2").progressBar(<?php echo $example1; ?>);
$("#pb3").progressBar(<?php echo $example2; ?>);
$("#pb4").progressBar(<?php echo $example3; ?>);
$("#pb5").progressBar(<?php echo $example4; ?>);
$("#pb6").progressBar(<?php echo $example5; ?>);
});
</script>
I hope it helps! Good Luck!
If you meant to pass php variable to client to dynamically replace numbers in jquery you can use like code in below
<script type="text/javascript">
$(document).ready(function() {
$("#pb1").progressBar(<?php echo example; ?>);
});
</script>
First off, having it in your database does not mean you cannot do so in the script. There are in fact several ways to do that. You could have your javascript file be php that generates javascript instead of html, or perhaps easier: you could use inline javscript. However, if you do want to keep the two separated it is still possible:
[..]
<script type='text/javascript'>var percentageDone = <?php echo intval($example) ?>;</script>
<script type="text/javascript">
$(document).ready(function() {
$("#pb1").progressBar(percentageDone);
});
</script>
[..]

how get php respone from jquery .load

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.

Session var not accessible in my script

I have two $_SESSION variables impossible to access in any script of my page but it's sure they exist in the PHP code of the same page when I use echo to display their values.
I can display in jQuery every classical PHP variables I want with the following code, but it's impossible when they are $_SESSION variables :
<?php if( isset($_SESSION['id']) ){
echo $_SESSION['id']; // it displays the value
} ?>
<script type="text/javascript">
$(document).ready(function() {
$("#some_button").click(function(){
var a = <?php echo $_SESSION['id']; ?>;
alert(a);
});
});
</script>
I don't understand why honestly...
If you are using PHP 5.2.0 or later, change this:
var a = <?php echo $_SESSION['id']; ?>;
To this:
var a = <?php echo json_encode($_SESSION['id']); ?>
That will put quotation marks around the result if necessary and escape characters for JavaScript as needed.
If you want to use something earlier than PHP 5.2.0, you can do something like this:
var a = '<?php echo $_SESSION['id']; ?>'
Ideally, though, you'd want to use a regexp and/or escaping/replacing functions unless you know that $_SESSION['id'] will only have safe characters. json_encode() has that stuff baked in already, so it's preferable.
I don't know if the example is in the same page but i suspect you 're missing session_start() so you can't use session variables
It is working to me:
Ready and test the code below, it is quite similar to your code, but I think you forgot to call jquery api.
<?php>
session_start();
$_SESSION['id'] = 1;
if ( isset($_SESSION['id']) )
{
echo $_SESSION['id'];
echo json_encode($_SESSION['id']);
}
?>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#some_button").click(function(){
var a = "Result: "+ <?php echo json_encode($_SESSION['id']); ?>;
//var a="<?php echo $_SESSION['id']; ?>";
alert(a);
});
});
</script>
<form method="post" name="form" action="#">
<input type="submit" id="some_button" value="Clique" />
</form>
If its a string:
var a= "<?php echo $_SESSION['id']; ?>";

Session variable does not get updated within javascript code

delete_define.php has the following code snippet:
<?php
session_start();
?>
<form action="delete_now.php" target="upload_target" onsubmit="return my_func_1();">
<input type="submit" name="my_submit" class="my_submit" value="submit"/>
<iframe id="upload_target" name="upload_target" src1111="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
<script type="text/javascript">
function my_func_1(){
//alert("from within my_func() =" +<?php echo $_SESSION['my_session']; ?>);
alert(" my_func_1");
return true;
}
function my_func_2(){
alert("my_func_2 =" +<?php echo $_SESSION['my_session']; ?>);
return true;
}
</script>
delete_now.php has:
<?php
session_start();
$_SESSION['my_session']=rand();
?>
<script type="text/javascript">
alert("from within delete_now.php = " +<?php echo $_SESSION['my_session']; ?>);
window.top.window.my_func_2();
</script>
The problem is my_func_2() does not give the same output for the session variable as the alert box in delete_now.php gives.
Why is that?
EDIT: CHANGED THE CODE SAID TO BE IN delete_define.php
That's because when the delete_define.php was loading the Session var was one, then it's become another, but in you JS stored previous value.
You should store session var into JS var, and then in JS in delete_now.php reset it with the fresh value.
How to refresh value from frame and other situations
Add to first php file's JS something like:
var session_var = '<?php echo $_SESSION['my_session']; ?>';
And then in your delete_now.php's JS:
parent.session_var = '<?php echo $_SESSION['my_session']; ?>';
And change function my_func to alert session_var JS variable.
Think so...
Explanation:
Then result page js will be:
function my_func_2(){
alert("my_func_2 = 13513513513513");
return true;
}
So when you call it, whatever is in the $_SESSION is, there will be old, static value.
Overall process description:
Load delete_define.php
Javascript var, containing actual filename initialized
From submits
Script delete_now.php is runing
Javascript var in main window refreshes
You call my_func_2() which use you global JS var, containing fresh filename.
you have assigned random number, so based on the order you run the page will give alert,
if you start from delete_now.php two consecutive alerts will be same . first assign some static value and then check

Jquery .each method

learning Jquery and integrating with PHP - getting there, but have one last challenge in some code I'm working on.
I have HTML in a string, trying to pull html in tags, might be multiple elements in the HTML string, so trying to use each. My function worked fine without each, below is my each integration (returns nothing currently):
<?php
$info = '<li><strong>I want this text</strong></li><li><strong>I want this text too</strong></li>';
$info = json_encode($info);
?>
<script type="text/javascript">
$(document).ready(function () {
$("a", $( < ? php echo $info; ? > )).each(
function () {
alert($(this).html());
});
};
This code below does work, but only returns the first element in the HTML:
<?php
$info = '<li><strong>I want this text</strong></li>';
$info = json_encode($info);
?>
<script type="text/javascript">
$(document).ready(function () {
var output = $("a", $( < ? php echo $info; ? > )).html();
var link = $("a", $( < ? php echo $info; ? > )).attr("href");
alert(output);
alert(link);
});
</script>
This is a description and a working example of How to use .each() LINK
You can try this one as a example
$("a").each(function(index){alert($(this).html()});
Your code is not working because there are a few syntax issues.
First, change
< ? php echo $info; ? >
to
<?php echo $info; ?>
PHP doesn't like spaces and the opening and closing tags must appear without spaces.
Second, close the ready function and the script tag properly. Instead of
};
use,
});
</script>
Why are you encoding a piece of XML with JSON? That makes like no sense at all. Both are ways to encode data. HTML is XML too, btw. You can directly reference the $info variable since PHP will process everything first on the server.
<?php
$info = '<li><strong>I want this text</strong></li>';
?>
<script type="text/javascript">
$(document).ready(function() {
$("a", $("<?php echo $info; ?>")).each(
function () {
alert($(this).html());
}
);
});
Or just remove the temporary variable altogether. Makes it combersome to read, but that's essentially what PHP is doing.
$("a", $("<?php echo '<li><strong>I want this text</strong></li>'; ?>")).each(
Or to make it even simpler, since you already have the HTML, simply include it as part of the page and maybe give it an ID to make referencing it easier with jQuery.
<li id="myList">
<strong>I want this text</strong>
</li>
<script type="text/javascript">
$(document).ready(function() {
$("a", "#myList").each(function() {
alert($(this).html());
});
});
</script>
This last example gets rid of PHP completely, but you weren't really using it anyways.

Categories