0

I have the following setup: JQuery:

$('.btn-setting').click(function(e){
        e.preventDefault();
        $('#myModal').modal('show');
    });

Which opens a HTML file as a Dialog box, the problem is that I cannot pass any PHP variables onto the page for reference:

<a href="<?= echo $UserID; ?>" class="btn btn-info btn-setting">Click for dialog</a>

Anyone suggest a method to enable me to do so?


Alright. The problem is now, i'm viewing the source of the page.. Yet, it's showing what i'm expecting, now When I click the button to bring up the dialog box, it's showing different data...

Not recommended I know, but heres a live:

92.236.219.136/Admin/

User: Stack

Pass: stack

3
  • 1
    <?= echo doesn't look right, <?= is echo already. Commented Oct 28, 2013 at 23:14
  • Are you trying to pass PHP into the modal window? Or just that link href? Commented Oct 28, 2013 at 23:16
  • @Revent I'm trying to pass a PHP variable into the modal window, so from User_Settings.php I can query the database to make changes to a user account Commented Oct 28, 2013 at 23:17

4 Answers 4

2

This is the most robust way, especially if you want to pass more than just one string:

<?php

// Call this function to pass an object to the JS code
function php_vars_to_js($id, $obj) {
  echo "<script id='$id' type='text/php_data'>";
  echo htmlspecialchars( json_encode( obj ) );
  echo '</script>';
}

// for examlpe
php_vars_to_js( "php_vars", array("a_string" => "foo", "a_num" => 12 ) );

And then read it this way:

var php_vars = $.jsonDecode( $("php_vars").text() );
Sign up to request clarification or add additional context in comments.

6 Comments

Keep in mind, that this is being queried from a HTML/PHP page included, the entire content is not being creating by jquery
I don't understand your comment. This function is just a safe way to pass objects from the server to the client. You can call it multiple time, with different ids, and assign the resulting object to whatever variable you want
Would it be alright if I show you a live version? Because i've encountered a weird problem.. On view source, i'm getting the correct information in the source. When I click, it doesn't display the same as the source
Sure, link the example
I have updated my question with a URL, sorry that it's an IP. It's a portforwarded web server which i'm locally testing, once logged in It's under "Developer List"
|
1

You can get the passed href in jQuery like this:

$('.btn-setting').click(function(e){
    e.preventDefault();
    var passedValue = $(this).attr('href');
    // passedValue contains your <?=$UserID?> from the link
    // ...
    $('#myModal').modal('show');
});

2 Comments

How would I beable to store this as a PHP var?
you'd need to send it to PHP using ajax
0
<a href="<?php echo $UserID; ?>" class="btn btn-info btn-setting">Click for dialog</a>

you were echoing the return of echo

4 Comments

That's not the question at all.
The question is a method to allow him to pass php variables to the output. Looking at the code, that's not working, so that's why I suggested another scripting 'method' "the problem is that I cannot pass any PHP variables onto the page"
@Mazzy two things, 1) her* and 2) Already tried using echos within the anchor reference, doesn't work.
1. Have you looked at the source after you ran it? 2. var_dump($UserID);
0

You can use tha data attribute, like this:

<a href="<?php echo $UserID; ?>" data-variable1="<?php echo $UserID; ?>" class="btn btn-info btn-setting">Click for dialog</a>

And in your JS:

$('.btn-setting').click(function(e){
        e.preventDefault();
        var1 = $(this).attr("data-variable1"); // Here is your PHP Variable now.. you can handle it to your Modal etc..
        $('#myModal').modal('show');
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.