PHP, requesting Javascript code in a PHP Echo [duplicate] - javascript

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; ?>';

Related

Put php code inside of registerScript of yii framework [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 2 years ago.
I want to put a php code inside of Yii::app()->clientScript->registerScript of yii1
How can I put it inside of this one?
<?php Yii::app()->clientScript->registerScript("form", <<<JAVASCRIPT
// I want the PHP code inside of this
JAVASCRIPT, CClientScript::POS_END); ?>
is there a way besides of ending the PHP in the middle of the code?
EDIT
if I put <?PHP ?> in the middle I'm getting an error of
Parse error: syntax error, unexpected end of file in
C:\xampp\htdocs\yii\project\protected\views\group_Form.php
on line 275
and that line is
JAVASCRIPT, CClientScript::POS_END); ?>
Since you can access the controller instance via $this in view context, I would suggest you doing something like that:
Create partial view php file where you can build your mixin (js + php) which obviously will contain any script type with some conditions on top of PHP.
Use CBaseController#renderPartial (which actually returns string instead of rendering, according to 3rd parameter return = true) in view context to get the mixin view contents as string and pass it as 2nd parameter to Yii::app()->clientScript->registerScript.
The implementation would look like this:
// _partial_view.php
// here is your mixin between js + php
/* #var $this CController */
<?php
echo '<script>' .
Yii::app()->user->isGuest
? 'alert("you\'re guest")'
: 'alert("you\'re logged user")'
. '</script>';
Then go back to your registering js invocation:
<?php Yii::app()->clientScript
->registerScript("form",
$this->renderPartial('_partial_view.php', [], true), // setting 3rd parameter to true is crucial in order to capture the string content instead of rendering it into the view
CClientScript::POS_END); ?>

How to execute PHP function with jquery variable [duplicate]

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>

Is there a way to access a javascript variable and its value using php? [duplicate]

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 );
});

Javascript to display PHP value with one second delay [duplicate]

This question already has answers here:
PHP loop; how to print each result and delay it for a second before echoing another result?
(2 answers)
Closed 8 years ago.
I have something like this
<?php
for ($x=0; $x<10; $x++) {
echo $x;
}
?>
Now i need to display that $x data one after another with one second delay; i believe i need javascript to do that.
I've found various of similar scripts, but none worked for me, can anyone post any example on how to do that?
Thanks in advance
Yes, you'll need JavaScript for this. All PHP can do is emit the values to client-side code. Then it's up to the client-side code to display those values.
So, for example, your PHP code can populate a JavaScript array:
<script type="text/javaScript">
var values = <?php echo json_encode($php_variable); ?>;
</script>
In this case $php_variable would be an array containing the values you want to display. Now your client-side code has an array called values which it can iterate. You can use setTimeout to schedule something to happen with a delay. Iterating over that array, it might look something like this:
var index = 0;
var displayValue = function () {
if (index >= values.length) { return; }
var value = values[index++];
// display "value" somewhere on your page
setTimeout(displayValue, 1000);
};
displayValue();
This should display each value in the array with 1-second increments. Note that I sort of glazed over the "display somewhere on your page" part, since that's up to you really. Where/how are you looking to display this? This works a little differently client-side than it does server-side, since the entire HTML document is already rendered. You need to identify where in that document you want to display the value and display it there, not just "echo" it.

Pass server-side variables to Javascript in PHP [duplicate]

This question already has answers here:
Efficient way to Pass variables from PHP to JavaScript [duplicate]
(8 answers)
Closed 9 years ago.
What is the best way to pass a server-side PHP variable to Javascript?
To simplify the problem assume that we have a variable in PHP ($phpVar) and we want to assign its value to a Javascript variable (jsVar)
Javascript files are loaded in html - they are not created dynamic!
Some food for thought:
1. Print with PHP before loading Javascript files:
<script language="javascript" type="text/javascript">
var jsVar= <?php echo $phpVar?>;
</script>
2. Store in DOM (in hidden elements)
a. in PHP:
<span data-name="phpVar" data-value="<?php echo $phpVar?>"></span>
b. Read in Javascript files (assuming jQuery available):
var jsVar= $('span[data-name="phpVar"]').attr('data-value');
3. Ask it with AJAX after page has loaded
Obviosly not the best solution. Doesn't fit to all scenarios and requires an additional request...
In conclusion:
They both seem ugly to me... Is there a better approach?
Is there any frameworks that can handle this dependecies? Please keep server reconfiguration minimal.
The best approach would be providing an "internal API" requested via AJAX from client side.
Doing this way you can keep your sides separated.
After this, the fastest way is printing in a shared file the values you want to share (as you wrote in your question).
As a last note: if you carry on with the second way I would suggest you
json_encode()
as a really helpful method to pass arrays and objects from php to javascript.
So if you have your php array:
$array = array( "a" => 1, "b" => 2 );
<script language="javascript" type="text/javascript">
var js_array= <?php echo json_encode( $array ) ; ?>;
</script>
It depends on the situation, but in common, predominantly to use first case. But don't forget about quotes if you pass a string:
<script language="javascript" type="text/javascript">
var jsVar = '<?php echo $phpVar?>';
</script>

Categories

Resources