This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
I'm not sure if this is even possible, but here goes.
I have a PHP function called code_check_result($code_name). It checks in the database and returns a bunch of information regarding the supplied code's name.
The name of the code is found within a span tag in the page. I've made a javascript function that opens a new window which should then execute the PHP function. However, the variable needed in the PHP function is stored in a jquery var as I have selected the span tag and stored whatever's inside.
Below you can see the part of the code where the window is created (just the part with the PHP function).
myWindow.document.write('Tjek: ' + code_name + '</br></br></br></br><?php code_check_result("kode2"); ?>');
"kode2" is the actual name of a code. The above works exactly as it's supposed to. However, if I substitute the "kode2" string with the javascript variable, 'code_name', it doesn't work. Like this:
myWindow.document.write('Tjek: ' + code_name + '</br></br></br></br><?php code_check_result("' + code_name + '"); ?>');
The first occurence of code_name writes it to the page, but the second one within the PHP function doesn't work.
I'm pretty sure this is just because the PHP is executed before the 'code_name' variable is written as a string. But I'm not entirely sure.
So, I'd appreciate it if you could either help me figuring out how to do this, OR tell me to abandon this idea because it's not possible and then give me some pointers as to how else to solve this.
Given this:
the variable needed in the PHP function is stored in a jquery var
and this:
I've made a javascript function that opens a new window
It sounds like you just need to pass a value to the URL being opened in that window. So where you might have something like this:
window.open('somePage.php');
You would instead have this:
window.open('somePage.php?someValue=' + myVariable);
The URL of the window being opened is a normal URL like any other. You can include values on the query string, and the PHP code in somePage.php can read them from the$_GET collection.
It will be easier to create $_POST variable with jQuery. Something like this:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$.ajax({
type: "POST",
url: "[URL GOES HERE]",
data:{ VARNAME: VALUE },
success: function(data){
console.log(data);
}
})
</script>
Related
This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 5 months ago.
as stated, I am trying to dynamically populate a [shortcode] value in WordPress so that it puts the appropriate value in the field before it dumps the shortcode and all of the scripting happens in the plugin that outputs the complex code.
as of right now, I am able to properly output the value i want as seen here:
var url = window.location.href;
url = url.split('#').pop().split('?').pop().split('docsid=').pop();
var page = url.substring(url.lastIndexOf('/') + 1);
var docsid = ('"' + page + '"')
console.log(docsid);```
This works as expected, my problem is, i'm trying to populate this field:
document.write(real3dflipbook id=docsid);
based on the code that the shortcode is dumped to, it looks like the shortcode is running first, then attempting to run my inputted javascript.
I have to console.logs running; one within the page itself, another in the embed.js file
the embed.js file console.log is working as it should be and that is the value output i'm hoping to get from my code.
1_633dbda152374 embed.js:144:16
the documents console.log is kicking up an error that it is producing from the embed.js file whenever i try to document.write(real3dflipbook id=docsid)
Uncaught SyntaxError: expected expression, got '<' documents:607:15
The code from the embed.js that I believe is where it is getting hung up is here:
var containerClass = bookContainer.attr("class")
var containerId = bookContainer.attr("id")
bookContainer.removeClass(containerClass).addClass(containerClass + "-" + containerId)>
console.log(containerId);
here is what the output code ends up looking like in the final page:
don't have enough points to embed yet
the top bit of highlighted garbage in the image is what happens when i attempt to document.write();
the bottom bit highlighted in blue is what SHOULD happen if it were properly updating the shortcode.
Any ideas or solutions on this? any help would be greatly appreciated!!
You can not just insert a shortcode parameter via javascript. The shortcode is rendered by the backend on page load. To dynamically set shortcode params you would need to make an AJAX call to a custom function in the backend which returns your shortcode with the specified parameters.
More information on Wordpress AJAX can be found here https://codex.wordpress.org/AJAX_in_Plugins
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 5 years ago.
So I need to request an image from a database, this then gets put in a variable, and gets called up in an echo.
However, i'm in a shortage of quotemarks in this case, seeing as the PHP echo uses double quotes, And then after calling up the onmouseover I use the single quotemarks, but after that I cannot use the double quotemarks again for the URL that gets caled up,
Going around this by putting the whole command in a variable didn't work.
Putting the whole command into the database didn't work.
Putting the command or url in a shorthand if statement didn't work.
And putting the Javascript code into a function, Also does not work.
Here is the code I'm talking about.
$afbeeldingurl = $row["afbeeldingurl"];
$overurl = $row["overafbeelding url"];
echo "<div><a href='../".$url.".html'><img onmouseover=mouseover() src='images/".$afbeeldingurl."' alt='' /></a>";
<script>
function mouseover()
{
this.src='images/<?php$overurl?>';
}
</script>
I thank you in advance :)
(Note, Only Javascript allowed, I cannot call up Jquery)
this.src='images/<?php$overurl?>';
Try switch this to
this.src='images/<?php echo $overurl; ?>';
This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 7 years ago.
I am currently looking for a way on how can I access a javascript variable and use it in my php script (same page). I heard about using ajax, but I don't really know where to start, I have little experience when using ajax, could someone please help me start with this problem? The code below is just an example on what I am trying to do:
<script>
var fruit = "apple";
</script>
I am trying to get the value of the variable fruit for example using a php script
<?php
$fruit = code to get the value maybe....
?>
My example above must be wrong and I know ajax is the way to go, but could someone please help me on how to approach or start with it? I would appreciate it! Thanks!
in the js you use ajax through jquery to send the vars to php
var fruit = "apple";
$.get('server.php',{fruit:fruit});
in server.php
$fruit = $_GET['fruit'];
of course opening server.php directly will cause an error, you have to run the javascript code in the browser, use the developer tools to inspect the request and the php file output.
Even better you can get back the server.php output in javascript like this:
var fruit = "apple";
$.get('server.php',{fruit:fruit})
.done(function( data ) {
alert( "Data Loaded: " + data );
});
I have http://localhost/?val=1
When I click on a link, is there a way this link can append a query variable to the current string, for example:
Link
so when I click it the url would be:
http://localhost/?val=1&var2=2
but when I click the link it removes the first query string and looks like
http://localhost/&var2=2
Is such a thing possible with normal HTML?
You can't do that using only html, but you can do it with js or php:
Using JS:
<a onclick="window.location+=((window.location.href.indexOf('?')+1)?'':'?')+'&var2=2'">Link</a>
Using Php:
Link
Notice 1: make sure you don't have the new variable in the current link, or it'll be a loop of the same variable
Notice 2: this is not a professional way, but it could work if you need something fast.
Basically you want to get your current URL via JavaScript with:
var existingUrl = window.location.href; // http://localhost/?val=1
Then append any Query Strings that are applicable using:
window.location.href = existingUrl + '&var2=2';
or some other similar code. Take a look at this post about Query Parameters.
Note: A link would already have to exist with an OnClick event that calls a function with the above code in it for it to work appropriately.
Now obviously this isn't very useful information on it's own, so you are going to want to do some work either in JavaScript or in Server code (through use of NodeJS, PHP, or some other server-side language) to pass those variable names and their values down so that the button can do what you are wanting it to do.
You will have to have some logic to make sure the query parameters are put in the URL correctly though. I.E. if there is only one query param it's going to look like '?var1=1' and if it's any subsequent parameter it's going to look like '&var#=#'.
I'm a relatively light JavaScript user, so this minor operation is giving me some major grief -- I'm sure I'm missing something.
All I want is a popup to open, passing one value -- the emplogin, a string variable called getEmp -- to a bog-standard .php page thusly:
<script type="text/javascript">
function getEmployeeInfo(getEmp)
{
window.open ("../Pages/Employee_Info.php?emplogin=" + getEmp + ",\"mywin\",\"menubar=0,resizable=1,scrollbars=1,width=600,height=450\"");
}
</script>
And it works... sort of. Testing the result by looking at the resulting $_REQUEST object shows me that the page is receiving the $emplogin as
Array
(
[emplogin] =>JohnDoe,"mywin","menubar=0,resizable=1,scrollbars=1,width=600,height=450"
)
IOW, the second and third parameters for Window.Open() are being passed along as part of the $emplogin received by PHP, instead of being parsed by JavaScript! (I'm using 'echo htmlspecialchars(print_r($_REQUEST, true));')
I'm sure there's something I'm not doing right with the escaping of the parameters but I haven't been able to hit on the right search terms. Thank you for any and all guidance!
Try this:
<script type="text/javascript">
function getEmployeeInfo(getEmp)
{
window.open ("../Pages/Employee_Info.php?emplogin=" + getEmp, "mywin", "menubar=0,resizable=1,scrollbars=1,width=600,height=450");
}
</script>
You're including your second and third parameters as part of your URL string, so it's all being treated as one single parameter.
What you want is:
window.open ("../Pages/Employee_Info.php?emplogin=" + getEmp,"mywin","menubar=0,resizable=1,scrollbars=1,width=600,height=450");
That is how it should be:
<script type="text/javascript">
function getEmployeeInfo(getEmp)
{
window.open ("../Pages/Employee_Info.php?emplogin=" + getEmp + ,"mywin","menubar=0,resizable=1,scrollbars=1,width=600,height=450");
}
</script>
urlencode your variable and remove the escaping of your other parameters in the open command