This question already has answers here:
GET URL parameter in PHP
(7 answers)
Closed 6 years ago.
I'm trying to get page number from web address like beds.html?page=3
but, if I use php get method, then it's not returning anything.
I hope to find any easy solution.
cheers
If You Want that parameter on html page then you have to use javascript for it
<script>
var param1var = getQueryVariable("page");
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
alert('Query Variable ' + variable + ' not found');
}
</script>
if you are on php page simply use
<?php
echo $_GET['page'];
?>
you can get page like this:
<?php
$_REQUEST['page'];
?>
and also you can type echo before $|_REQUEST to check you are getting proper page number or not
get is the right method to use.
<?php
$_GET['page'];
?>
Related
This question already has an answer here:
Count Number of Clicks on a link (Without onclick) [closed]
(1 answer)
Closed 4 years ago.
i want to count clicks when this link is clicked. AND show "how many times(number) link clicked"in example.com
not to use mysql.and better php if not,then javascript is ok
example.com
For a pure JS implementation, you could use localStorage as follows:
<html>
<script>
function init() {
let count = localStorage.getItem('counter');
if(count === null){
count = 0;
localStorage.setItem('counter', count);
}
count = parseInt(count);
updateCount(count);
}
function incrementCounter() {
let count = parseInt(localStorage.getItem('counter'));
count = count + 1;
localStorage.setItem('counter', count);
updateCount(count);
return true;
}
function updateCount(count) {
document.getElementById("count").innerHTML = "Clicked "+count+" times!";
}
</script>
<body>
<p id="count">-</p>
<script type="text/javascript">
init();
</script>
Google
</body>
This question already has answers here:
how to convert javascript variable to PHP variable? [closed]
(2 answers)
How to get JavaScript function data into a PHP variable
(5 answers)
Closed 9 years ago.
i am trying to get the javascript variable value to php.
here is my javascript code:
function getResults()
{
var radios = document.getElementsByName("address");
for (var i = 0; i < radios.length; i++)
{
if (radios[i].checked) {
var a = radios[i].value
alert(a);
break;
}
}
}
from this javascript i want to get variable a's value inside php code onclick on submit button.
how can i do this?
i tried this
$var1 = $_GET["a"];
Do this..
$var1 = <?=$_GET["a"]?>;
to communicate a value from JavaScript to PHP do following
$var1 = <?=$_POST["a"]?>;
What you tried is almost correct:
var a = <?=$_GET["a"]?>;
Since javascript code runs on client-side, but PHP is server-side code, you need to send your JS variables to the server. You can do this using AJAX.
Hint: AJAX calls usually use POST instead of GET.
Replace:
$var1 = $_GET["a"];
on:
$var1 = <?=$_POST["a"]?>;
This question already has answers here:
jQuery simple value get issue with .val()
(3 answers)
Closed 9 years ago.
thanks for taking the time. I'm working on building a visual shopping cart into multiple pages of a parallax site. Because each 'page' of the parallax forces open and close a form, I cannot use a continuous form. To remedy this, I am using jquery to place the value of the choices on the preceding pages into one page, filling spans.
I'd like to be able to grab these final choices from the dom using ajax to put them to a processing page to store the customers choice information, and check it against a dwolla transaction. I haven't got to setting up the dwolla transaction and check yet, because I have not been able to get my data to the php processing page, and get it to return an echo-able value. This is built off wordpress by the way, and allot of times, filters wordpress is implementing screw me up.
Here's the site; http://ecigjuiceclub.com
Lets jump into the code!
AJAX:
function post()
{
var name = $('#cname').val();
var street1 = $('#cstreet').val();
var street2 = $('#cstreet2').val();
var city = $('#ccity').val();
var state = $('#cstate').val();
var zip = $('#czip').val();
var email = $('#cemail').val();
var phone = $('#cphone').val();
var flavor = $('#cflavor').val();
var strength = $('#cstrength').val();
var refcode = $('#crefcode').val();
$.post('process.php',{postname:cname,poststreet1:cstreet,poststreet2:cstreet2,postcity:ccity,poststate:cstate,postzip:czip,postemail:cemail,postphone:cphone,postflavor:cflavor,poststrength:cstrength,postref:crefcode},
function(data)
{
$('#thankyou').html(data);
});
}
Php process.php
<?php
$name = $_POST['postname'];
$street1 = $_POST['poststreet1'];
$street2 = $_POST['poststreet2'];
$city = $_POST['postcity'];
$state = $_POST['poststate'];
$zip = $_POST['postzip'];
$email = $_POST['postemail'];
$phone = $_POST['postphone'];
$flavor = $_POST['postflavor'];
$strength = $_POST['poststrength'];
$refcode = $_POST['postref'];
if($zip == 90804)
{
echo "1";
}
else
{
echo "0";
}
?>
HTML Snippet (where in the DOM I'm attempting to grab):
<span style="color:#EF5D3D;">Email: </span><span style="font-size:28px;" id="cemail"></span>
<span style="color:#EF5D3D;">Phone: </span><span style="font-size:28px;" id="cphone"></span>
i think you better try innerHTML instead values i dont know the exact syntax in jquery. iprefer u use input or else instead span
var email = $('#cemail').text();
var phone = $('#cphone').text();
These should be the replacements on the post() function. Adding an alert message (as an example) at the opening of the post() function could help you check their values before they get passed in the Ajax request.
alert($('#cemail').text() + ' ' $('#cphone').text());
Also make sure to clear your cache just to make sure your javascript edits have been implemented.
The console is showing the following errors which may or maynot prevent other scripts from running:
Uncaught ReferenceError: Modernizr is not defined
ecigjuiceclub.com/:825
Blocked a frame with origin "https://www.youtube.com" from accessing a
frame with origin "http://ecigjuiceclub.com". The frame requesting
access has a protocol of "https", the frame being accessed has a
protocol of "http". Protocols must match.
This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 9 years ago.
Yes I know this question gets asked a lot, but I'm fairly new to JS and I need to use a php variable in some JS. I'm more then aware that PHP is executed server side and JS is client side however other people claim that this works.
I've got a PHP variable called "test1" that I want to log to the JS console (for instance):
<?php
$test1 = '1';
print '
<script type="text/javascript">
var carnr;
carnr = "<?php print($test1); ?>"
console.log(carnr);
</script>';
?>
What this does is print " " to the JS console. Not exactly what I was hoping for.
Now this may not even be doable and I may have to pass the variable off the page and back in again with AJAX, but I'd rather have a quick and easy solution if there is one available!
Any help is appreciated.
You could do this.
<script>
var JSvar = "<?= $phpVar ?>";
</script>
The PHP will be parsed and the value of $phpVar will become the value of JSvar whatever.
Make sure you encode phpVar properly. For example, if phpVar contains a double quote, you'll end up with a broken JS
Use this no need to give "" => change to '.$test1.'..
<?php
$test1 = '1';
print '
<script type="text/javascript">
var carnr;
carnr = "'.$test1.'"
console.log(carnr);
</script>';
?>
try
<?php $test1 = '1'; ?>
<script type="text/javascript">
var carnr;
carnr = "<?php print($test1); ?>"
console.log(carnr);
</script>
Generally, it is better to not print static stuff with php, but to have static (that is unchanging) stuff directly in HTML and only use PHP on the parts that really need it.
You made a mistake do it so:
<?php
$test1 = '1';
echo '<script type="text/javascript"> var carnr; carnr = "'.$test1.'" console.log(carnr)</script>';
?>
Since you're writing your JS with your PHP, you can simply do:
$test1 = "blah";
echo "<script type=\"text/javascript\">console.log($test1);</script>";
You are already in open php tag. When printing the line just append the variable to the output by using a dot.
Example:
print 'variable1 '.$variable1.' is now printed';
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
javascript - document.write error?
I have a piece of Javascript that I'm using that checks to see if a div is a certain style and then runs an if else statement:
window.onload = function () {
if (document.getElementById('slidingDiv').style.display == 'none') {
document.write('<img src="images/show-more-arrow.jpg" width="61" height="45">');
} else {
document.write('<img src="images/show-less-arrow.jpg" width="61" height="45">');
}
};
This works OK, but it only shows this HTML image on the screen. I need it to add the HTML to the existing code on the page.
Many thanks
Use appendChild or innerHTML
example:
document.getElementById("elemId").innerHTML = '<img src="images/show-less-arrow.jpg" width="61" height="45">'
var wrapper = document.createElement('div');
var image = document.createElement('img');
image.src='images/show-more-arrow.jpg';
wrapper.appendChild(image);
Something like this.