Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php in javascript

If your whole JavaScript code gets processed by PHP, then you can do it just like that.

If you have individual .js files, and you don't want PHP to process them (for example, for caching reasons), then you can just pass variables around in JavaScript.

For example, in your index.php (or wherever you specify your layout), you'd do something like this:

<script type="text/javascript">
    var my_var = <?php echo json_encode($my_var); ?>;
</script>
You could then use my_var in your JavaScript files.

This method also lets you pass other than just simple integer values, as json_encode() also deals with arrays, strings, etc. correctly, serialising them into a format that JavaScript can use.
Comment

use js in php


<?php 
echo '<script type="text/JavaScript"> 
     prompt("GeeksForGeeks");
     </script>'
;
?>
Comment

php js

function pp_check_reactapp_ajax() {

    $request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
    $request->set_header( 'content-type', 'application/json' );
    $request->set_body( '{
            "title": "'. $_POST['title'] .'"
        }');

    $response = rest_do_request( $request );

    $server = rest_get_server();
    $data = $server->response_to_data( $response, false );

    if($data['id']){
        echo '{"success": '. json_encode($data) .'}';
    } else {
        echo '{"error": "'. $data['message'] .'"}';
    }

    wp_die();

}
add_action( 'wp_ajax_nopriv_pp_check_reactapp_ajax', 'pp_check_reactapp_ajax' );
add_action( 'wp_ajax_pp_check_reactapp_ajax', 'pp_check_reactapp_ajax' );
Comment

PREVIOUS NEXT
Code Example
Php :: laravel blade dump 
Php :: is_page () 
Php :: message mkdir() invalid path filename drivers/session_files_driver.php 
Php :: laravel token logout 
Php :: how to pass id through get template part 
Php :: php slugify 
Php :: get size files in laravel 
Php :: php get all values from associative array certain key 
Php :: wordpress get post by id 
Php :: save array in mysql php 
Php :: invalid datetime format 1292 
Php :: php start server command 
Php :: parametre grouping laravel quert 
Php :: laravel share on whatsapp link 
Php :: laravel 8 change password 
Php :: how to add recaptcha validation in php 
Php :: upload file in laravel 
Php :: json whereIn laravel 
Php :: laravel database select 
Php :: php timer 
Php :: how to display user id from a function on a wordpress page 
Php :: make model inside module laravel 
Php :: laravel migration change column type 
Php :: php remove last newline from string 
Php :: migration create symfony 
Php :: laravel eloquent select one column 
Php :: carbon get time 
Php :: register_post_type wordpress 
Php :: shortcut vsc select line up 
Php :: create char laravel migration 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =