Order now

How to execute php code in javascript

<button type="button" id="okButton" onclick="funk()" value="okButton">Order now </button> <script type="text/javascript"> function funk(){ alert("asdasd"); <?php echo "asdasda";?> } </script> 

When the button is pressed, I want to execute php code (at this point for asadasda echo)

+6
source share
8 answers
 function echoHello() {alert ("<?php funkx(); ?>");} 

This code should provide you with a popup

-16
source

You can use http://phpjs.org/ http://locutus.io/php/ it passes a bunch of PHP functionality to javascript, but if it's just an echo and the script is in a php file, you can do something like this :

 alert("<?php echo "asdasda";?>"); 

don’t worry about using double quotes interchangeably, PHP will do this before the browser sees it.

as for using ajax, the easiest way is to use a library like jQuery . With this you can do:

 $.ajax({ url: 'test.php', success: function(data) { $('.result').html(data); } }); 

and test.php:

 <?php echo 'asdasda'; ?> 

it will write the contents of test.php to any element of the result class.

+14
source

Javascript and PHP interaction

We all grew up knowing that Javascript works on the client side (that is, in the browser) and PHP is a server-side tool (that is, a server). It is PURE that both simply cannot interact.

But the good news; it can be made to work and that's how.

The goal is to get some dynamic information (for example, server configuration items) from the server to the Javascript environment so that it can be used if necessary - usually this means changing the DHTML in the presentation.

First, to clarify the use of DHTML, I will give this DHTML example:

 <script type="text/javascript"> function updateContent() { var frameObj = document.getElementById("frameContent"); var y = (frameObj.contentWindow || frameObj.contentDocument); if (y.document) y = y.document; y.body.style.backgroundColor="red"; // demonstration of failure to alter the display // create a default, simplistic alteration usinga fixed string. var textMsg = 'Say good night Gracy'; y.write(textMsg); y.body.style.backgroundColor="#00ee00"; // visual confirmation that the updateContent() was effective } </script> 

Suppose we have an html file with ID = "frameContent" somewhere, then we can change the display using a simple <body onload = "updateContent ()">

Golly gee; we don’t need PHP to do it now! But this creates a framework for using the PHP provided content.

We change the webpage in question to a type of PHTML to allow server-side access to PHP for content:

 **foo.html becomes foo.phtml** 

and we will add to the top of this page. We also cause php data to be loaded into global lists for later access - - like this:

 <?php global $msg1, $msg2, $textMsgPHP; function getContent($filename) { if ($theData = file_get_contents($filename, FALSE)) { return "$theData"; } else { echo "FAILED!"; } } function returnContent($filename) { if ( $theData = getContent($filename) ) { // this works ONLY if $theData is one linear line (ie remove all \n) $textPHP = trim(preg_replace('/\r\n|\r|\n/', '', $theData)); return "$textPHP"; } else { echo '<span class="ERR">Error opening source file :(\n</span>'; # $filename!\n"; } } // preload the dynamic contents now for use later in the javascript (somewhere) $msg1 = returnContent('dummy_frame_data.txt'); $msg2 = returnContent('dummy_frame_data_0.txt'); $textMsgPHP = returnContent('dummy_frame_data_1.txt'); ?> 

Now our javascripts can go to PHP global globals as follows:

// by accessing global variables var textMsg = '<? php global $ textMsgPHP; echo "$ textMsgPHP" ;? > ';

In javascript replace

var textMsg = 'Say good night kill';

c: // using php returnContent ()

var textMsg = '<? php $ msgX = returnContent ('dummy_div_data_3.txt'); echo "$ msgX"? > ';

Summary:

  • The webpage to be modified MUST be a phtml file or some php file.
  • the first thing in this file MUST be <? php to get dynamic data? >
  • php data MUST contain its own CSS style (if the content is in the frame)
  • javascript to use dynamic data must be in the same file
  • and we go to / from PHP as needed to access dynamic data
  • Please note: - use single quotes in external javascript and double quotes ONLY in dynamic php data.

It will be allowed: to call updateContent () with the file name and using it via onClick () instead of onLoad ()

An example can be provided in Sample_Dynamic_Frame.zip for your inspection, but did not find a means to attach it.

+5
source

You cannot run PHP using javascript. JavaScript is a client technology (works in a users browser), and PHP is a server technology (runs on a server).

If you want to do this, you need to make an ajax request to the PHP script and return the results you are looking for.

Why do you want to do this?

+3
source

If you just want an echo from PHP at a specific place on the page when the user clicks the button, you can do something like this:

 <button type="button" id="okButton" onclick="funk()" value="okButton">Order now</button> <div id="resultMsg"></div> <script type="text/javascript"> function funk(){ alert("asdasd"); document.getElementById('resultMsg').innerHTML('<?php echo "asdasda";?>'); } </script> 

However, assuming your script needs to do some server-side processing, such as adding an item to the cart, you might like to check jQuery http://api.jquery.com/load/ - use jQuery to load the path to the php script that performs the processing. In your example, you can do:

 <button type="button" id="okButton" onclick="funk()" value="okButton">Order now</button> <div id="resultMsg"></div> <script type="text/javascript"> function funk(){ alert("asdasd"); $('#resultMsg').load('path/to/php/script/order_item.php'); } </script> 

This starts the php script and loads any message that it returns into <div id="resultMsg"> .

order_item.php will add the item to the cart and simply display all the messages you want to display. To get an example of work, this will be enough, like order_item.php:

 <?php // do adding to cart stuff here echo 'Added to cart'; ?> 

To do this, you will need to include jQuery in your page by adding this to your <head> :

 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> 
+2
source

Maybe so:

  <?php if($_SERVER['REQUEST_METHOD']=="POST") { echo 'asdasda'; } ?> <form method="post"> <button type="submit" id="okButton">Order now</button> </form> 
0
source

If you do not want to include the jquery library, you can simply do the following

a) ad iframe, size 0px so that it is not visible, href is empty.

b) execute this in your js code function

  window.frames['iframename'].location.replace('http://....your.php'); 

This will execute the php script, and you can, for example, do a database update ...

0
source

put your php in a hidden div and call it using javascript

php part

 <div id="mybox" style="visibility:hidden;"> some php here </div> 

javascript part

 var myfield = document.getElementById("mybox"); myfield.visibility = 'visible'; 

now you can do something with my field ...

0
source

Source: https://habr.com/ru/post/925784/


All Articles