0

Basically I have a combination of PHP codes and javascript codes. My mySQL data are encrypted using CodeIgniter, thus to load the data (view and edit) in json, i need to decrypt it again. My question is how to make my "$x" variable dynamic?

Thanks.

    function edit_person(id)
    {
        save_method = 'update';
        $('#form')[0].reset();
        $('#modal_form').modal({backdrop: 'static', keyboard: true, show: true }); 
    
        <?php
        $x = 13; //<==  **i need to make this $x dynamic based on "edit_person(id)"** //
        $url = "http://myurlhere.com/main/ajax_edit/".$x;
        $datax = file_get_contents($url);
        $string = json_decode($datax, TRUE);
        ?>
        
        $.ajax({
            url : "<?php echo site_url('main/ajax_edit')?>/" + id,
            type: "GET",
            dataType: "JSON",
    
    success: function(data)
    {
    $('[name="id"]').val(data.id);
    // ** below code "firstName" is my decryption requirement ** //
    $('[name="firstName"]').val("<?php echo $this->encryption->decrypt($string['firstName']); ?>");
    $('#modal_form').modal('show');
    },
    error: function (jqXHR, textStatus, errorThrown)
    {
    alert('Error get data from ajax');
    }
  });
}

1 Answer 1

1

You are probably confusing the server-side and the client-side code. In simple terms: First, the client sends a request to the server. The target PHP code gets executed on the server-side only. It then generates an HTML file, which contains your JS code. This file is sent to the client and is executed on the client-side only. At that time, on the client-side, there is JS code only and no PHP code anymore. All the PHP code gets replaced by some value or is simply removed or ignored.

If you want to access some PHP functionality from your JS code, you have to send a request from the client to the server. As you are doing with the AJAX call.

So in order to make your $x dynamic, you have to call some PHP code and pass the ID as a parameter.

In a strongly simplifyed way you could achieve this by:

$.ajax({
            url : "your url to some file.php/?id=" + id,
            type: "GET",
    })

some file.php

 <?php
    $x = $_GET["id"]; //<== $_GET["id"] will return the value of the parameter "id" in the url
    ?>

Starting from here, you should read more about AJAX calls, input sanitation and validation in order to secure your requests.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the response. Perhaps the easiest to capture the needed PHP "$x" variable is to get the javascript function id . How do I get the javascript "id" in "function edit_person(id)" and use it as PHP variable.
You can't capture the JS function ID in the PHP code, since those are differen instances. Imagine it as if two person are communicationg over email. Person A is PHP, person B is JS. If you want to use ID in PHP, then JS has to send a get request to PHP, with the ID as a parameter. Then you can execute a PHP script, which will take the parameter and do something with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.