currently, I am trying to set the URL of my website base on what the user click. The website is coding in html and php and this is the code I am trying to use to set the Url. However it seems that windows.location doesn't recognize php code. is there any other way to do this?
<script>
$(document).ready(function(){
$("#HELP").click(function(){
window.location='hotels.php?Category=<?php $cat ?>&Pic=<?php $img ?>';
});
});
</script>
You forgot to echo the variables to the output:
window.location='hotels.php?Category=<?php echo $cat ?>&Pic=<?php echo $img ?>';
Variables by themselves don't emit anything to the page. They simply represent a value.
Try this:
<script>
$(document).ready(function(){
$("#HELP").click(function(){
var url = 'hotels.php?Category=<?=$cat?>&Pic=<?=$img?>';
window.location= url;
});
});
</script>
What change:
Echo php veriables in url
But its not a good practice to use php code in client side you can create global veriables in java script.
Or if your requirement is like that than you can also use a hidden value for url in html and than get value in your jQuery code as like that:
<input type="hidden" id="url" value="<?=hotels.php?Category=<?=$cat?>&Pic=<?=$img?>" name="url">
<script>
$(document).ready(function(){ $("#HELP").click(function(){
var url = $('#url').val();
window.location= url;
});
});
</script>
Related
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I have a button in my php page :
<button id="myButton">Delete me</button>
and in that page I have a variable which I want to pass to a JavaScript function, and this is my JS code :
<script>
$(function() {
$('#myButton').confirmOn('click', function(e, confirmed){
if(confirmed) {
//Here I'll use the variable
}
})
});
</script>
How can I do that ?
I think you might be wanting to put a variable through PHP on your button and pass it to your function using jQuery data. Check this out:
<button data-confirmed="<?php echo $confirmed; ?>" id="myButton">Delete me</button>
And in your js:
$(function() {
$('#myButton').on('click', function(e){
// get jquery object access to the button
var $thisButton = $(this);
// this gets that data directly from the HTML
var confirmed = $thisButton.data('confirmed');
if(confirmed) {
//Here I'll use the variable
}
})
});
Basically, you can access variables in javascript using this method if you are interpolating PHP vars directly on the page. If this isn't what you are looking for, please let me know in comments.
<button id="myButton">Delete me</button>
<input type="hidden" name="variable" id="variable" value=2>
<script>
$(function() {
$('#myButton').confirmOn('click', function(e, confirmed){
if(confirmed) {=
alert(document.getElementById('variable').value);
//Here I'll use the variable
}
})
});
You can declare the variable outside the click event like so:
$(function() {
var confirmed = true;
$('#MyButton').confirmOn('click', function() {
if(confirmed) {
// do stuff
}
});
});
Assuming you're talking about passing php variables to Javascript, you can do this when writing to the page, ex:
<?php
$passThis = 'Passing'
?>
<script language="javascript" type="text/javascript">
var sStr = "My name is <?php echo $passThis ?>.";
document.write(sStr);
</script>
You could also get integer values, doing something like
$integerValue = 5;
var int = "<?php echo $integerValue; ?>";
int = parseInt(int);
By modifying this, you could use it to pass more types of variables, so assuming you have something like this:
<?php
$text = 'someText';
?>
<script>
$(function() {
$('#myButton').confirmOn('click', function(e, confirmed){
if(confirmed) {
//Here I'll use the variable
}
})
});
</script>
you could do
<script>
$(function() {
$('#myButton').confirmOn('click', function(e, confirmed){
if(confirmed) {
console.log("<?php echo $text ?>");
}
})
});
</script>
to make Javascript alert 'someText'.
I'm newbie at Jquery. I stacked to passing php $_GET['myvalue'] to my jquery file.
I struggled very much but I cannot find solution.
I have different one php file and one jquery file. I call my php file like
myphpfile.php?myvalue=testvalue
Then I want to receive myvalue's value to my jquery file. I tried document.getElementById but It doesnt work.
For any helping Thanks.
If you want to access the $_GET['myvalue'] value in your Javascript you can echo it out straight into it.
<script type="text/javascript">
// Your code
// The var you want to assign it to
var value = '<?php echo $_GET['myvalue'];?>'
</script>
Try this in your jQuery File:
First create a function to parse the parameters in the URL:
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
vars[key] = value;
});
return vars;
}
Then you call the function:
var myUrlParameters = getUrlVars();
Then you can access them with:
var myParameter = myUrlParameters['myParameter'];
You need to read more about jQuery and Javascript...
You might want to add the variable you want in an HTML element, for example:
PHP
<?php
$myValue = $_GET["myvalue"];
echo '<input type="hidden" name="_method" value="'.$myValue.'" id="myElement" />';
?>
At jQuery:
$(document).ready(function(){
alert($('#myElement'));
});
If you want to access the $_GET['myvalue'] value in your script but using only Javascript.
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
I have an ajax function that post to an PHP file.
Now since I'm using WordPress I can use the get_url function so I don't need to hard code the entire URL.
The WordPress function is an PHP so I'm trying to use PHP inside the ajax post. But it wont do the trick.
Any ideas ? and is it possible ?
This is what I have.
$(document).ready(function(){
$('#submit').click(function(){
$.post('<?php echo get_template_directory_uri(); ?>/send.php', $("#mycontactform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
});
I have also tried the php echo inside quotes like this.
$.post(' "<?php echo get_template_directory_uri(); ?>" /send.php' ....
ether way I get this path
http://mysite.com/%27%3C?php%20echo%20get_template_directory_uri();%20?%3E%27/send.php&email=&message=&name=&sent=1
Javascript to PHP = nope you cant embed javascript to php, only php can embed html and javascripts.
the better way to do it is to create a .php file and insert the javascript there...
Example: js.php
<?php
function doSubmit() {
?>
<script>
$('#submit').click(function(){
$.post('<?php echo get_template_directory_uri(); ?>/send.php', $("#mycontactform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
</script>
<?php
}
?>
call the js.php using "include(js.php);" and call the functions inside another php
Inside your index.php
<?php
include('js.php');
?>
<html>
<head><script><?php doSubmit();?></script></head>
<body>
</body>
</html>
index.html
<script>
function check() {
var id = $('input[name=id]').val();
$("#result").load("/ajax.php", {id:id});
}
</script>
<input type="text" size="3" name="id"><br />
<button onclick="check();">Pay</button>
<div id="result"></div>
ajax.php
<?php
$id = (int)$_REQUEST['id'];
//some validations and SQL executions
echo "<script language=JavaScript src='https://*****/index.php?id=$id&invoice=$invoice&sum=$sum.......etc'></script>";
I try to form javascript in php file and embed it into div with id="result".
I've used get, load, ajax, createElement methods but the script doesn't execute.
try this..
$('#result').load('ajax.php');
In your php file ajax.php include the header.
header('Content-Type: text/javascript');
And in index.html add type=”text/javascript” to the script tag.
<script type=”text/javascript”>
function check() {
var id = $('input[name=id]').val();
$("#result").load("/ajax.php", {id:id});
}
</script>
You must load script as real script element. DOM conversion of "<script>/*...*/</script>" may fail in this case. This is done via standart DOM document.createElement function.
var script = document.createElement("script");
script.src = path;
Other way is to download the script via AJAX and then eval it.
Try using getScript:
$.ajax({
url: url,
dataType: "script",
success: success
});
try this:-
"<script language=JavaScript src='https://*****/index.php?id="'.$id.'"&invoice="'.$invoice.'"&sum="'.$sum.'".......etc'></script>";
Why wouldn't you load directly the script URL using AJAX?
ajax.php only should return path to the script:
$id = (int)$_REQUEST['id'];
//some validations and SQL executions
echo "https://*****/index.php?id=$id&invoice=$invoice&sum=$sum.......etc";
Besides that all HTML element attributes should be enclosed with quotes or apostrohpes.
Then you should load this url in javascript and generate script element here:
function loadScript(url,appendTo) {
var script = document.createElement("script")
script.type = "text/javascript";
script.src = url;
if(typeof appendTo.appendChild == "function") //Chceking if we can append script to given element
appendTo.appendChild(script);
else
document.body.appendChild(script) //This will not work in old IE, where document.body is not defined.
}
You call this function with script url and optional target element (where will be put in) as parameters.
Like this:
$.get("ajax.php",{param1:"value"},function(scriptUrl) {loadScript(scriptUrl, document.getElementById("result");})
I have a Google style instant search script written in jQuery which pulls results from a PHP script. I want to make a script so I can change the destination of the file by clicking a certain link. How can I make it so when a certain link is clicked it changes the selected.tab variable to the name of the search type. How can I do this?
Here is my jQuery script:
$(document).ready(function(){
$("#query").keyup(function(){
var query=$(this).val();
var yt_url=''+selected.tab+'.php?q='+query;
window.location.hash=''+selected.tab+'/'+query+'/';
document.title=$(this).val()+" - My Search Script";
if(query==''){
window.location.hash='';
document.title='My Search Script';
}
$.ajax({
type:"GET",
url:yt_url,
dataType:"html",
success:function(results){
$('#results').html(results);
}
});
});
if(window.location.hash.indexOf('#'+selected.tab+'/')==0){
query = window.location.hash.replace('#'+selected.tab+'/', '').replace('/', '');
$('#query').val(decodeURIComponent(query)).keyup();
}
});
From your code, the selected variable seems to be global, so:
<a id="change" href="#">change</a>
<script type="text/javascript">
$('#change').click(function() {
selected.tab = "somethingelse";
});
</script>