0

I have the following JS code:

<script>
    $('#mpxModalEdit').on('show.bs.modal', function(e) {
        var editId = $(e.relatedTarget).data('edit-id');
    $(e.currentTarget).find('input[name="editId"]').val(editId);
    });
</script>

This places the CORRECT edit-id value into a form text box name=editIdas I wish.

I would like to add another line of JS so that it ALSO places the value into a PHP variable since I need to make a subsequent

$query = "select * from playlists where id='editId'

3
  • 1
    You should use AJAX. Commented Oct 10, 2016 at 5:55
  • If you submit the form to the server, it will receive it. Better is as mentioned to AJAX the variable and receive the result without reloading Commented Oct 10, 2016 at 6:22
  • What people are trying to tell you in different ways is that the PHP environment can not be changed by javascript. You will have to send the information to 'another' file and process the information there. You are handing someone a book and after they read it you want their opinion printed in the book. That is not possible. You can add it in the next print though. You should use AJAX, grab the information needed and serve it to a container on the page. Commented Oct 10, 2016 at 6:41

3 Answers 3

2

I don't know any PHP syntax, but what I can tell you is that PHP is executed on the server and JavaScript is executed on the client (on the browser).

if on your page you had:

<form method="get" action="blah.php">
    <input name="test"></input>
</form>

Your $_GET call would retrieve the value in that input field.

So how to retrieve a value from JavaScript?

Well, you could stick the javascript value in a hidden form field... That could be the best solution only.

<script type="text/javascript" charset="utf-8">
    var test = "tester";
    // find the 'test' input element and set its value to the above variable
    document.getElementByID("test").value = test;
</script>

... elsewhere on your page ...

<form method="get" action="blah.php">
    <input id="test" name="test" visibility="hidden"></input>
    <input type="submit" value="Click me!"></input>
</form>

Then, when the user clicks your submit button, he/she will be issuing a "GET" request to blah.php, sending along the value in 'test'.

Or the another way is to use AJAX.

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

Comments

2

PHP-Scripts are only run, when you load your page before any js is run or make an AJAX. In addition, PHP runs on the server, while JS is client-side.
My first suggestion would be, to really think, whether you need to do this (or even tell us, why you think it is).
If you really need it, you can perfom an AJAX and send your variable as data to the Server.

4 Comments

It is needed in a modal - the edit-id value is working properly and being displayed in the input box, so the value is showing and being transferred to the modal. So it IS being placed in the input box - just want to know how to ALSO place this same value in a PHP variable during this same modal display??
@NetTemple Im trying to figure out, why do you need the value in a PHP-variable in the first place? How do you want to use this variable?
in an edit sql query: "$query = "select * from playlists where id='editId'"
@NetTemple So you want to load some content without reloading the webpage? Then AJAX is the way to go
0

Using AJAX call you can pass js values to PHP script. Suppose you are passing editId js value to logtime.php file.

$(document).ready(function() {

            $(".clickable").click(function() {
                var userID = $(this).attr('id');
                //alert($(this).attr('id'));
                $.ajax({
                    type: "POST",
                    url: 'logtime.php',
                    data: { editId : editId },
                    success: function(data)
                    {
                        alert("success!");
                    }
                });
            });
        });

<?php //logtime.php
$editId = isset($_POST['editId']);
//rest of code that uses $editId
?>
Place the AJAX call after

$(e.currentTarget).find('input[name="editId"]').val(editId);
line in your js script. then you can assign to your desired PHP variable in logtime.php file

2 Comments

Not understanding where logtime.php is coming from. I have a pop-up modal. Is this representing the parent page?
replace logtime.php with your file URL where you want to pass the js variable

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.