0

i just want to know how to send a variable from the php page to html page ?

this is my user_check.php code :

<? php
$var="1021";
?> 

and my user.html code is

<div id="var" type="hidden"/>

i just want to pass the variable $var to the div control and need to store the variable in value attribute of div . i searched all the google but no solutions available . can anyone help ? Thank you

4
  • Echo it. And why do you want to hide it? Commented Aug 24, 2015 at 5:23
  • do you want feature like mvc ?? Commented Aug 24, 2015 at 5:25
  • i want to show it to another php page not in the same html Commented Aug 24, 2015 at 5:40
  • yes MVC @ShowStopper Commented Aug 24, 2015 at 5:49

2 Answers 2

1

you can use ajax to get the data like this . use this function in you user.html

<div id="var" type="hidden"/>
$(document).ready(function(){
    $.ajax({
            url: 'user_check.php',   // link your page
            type: 'POST',
            data: {
                //'key1': data1,
                // 'key2': data2
                //if you want to post somethig 
            },
            success: function(data) {
               // got success data
               $('#var').html(data);   // set value in the div
            },
            error: function(data) {
               // if error occure
                alert('Some Error Occurred. Please Try Later.');
            }
        });
});

in your user_check.php echo what you want to show . as ajax will display the data which will print

<? php
echo $var="1021";
?>

Note you may need to include jquery library for this

if jquery is not present . include this cdn

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

if you want to know more about how .ajax work follow this refrence or refrence2

update

Another way to do this is include user.html after variable declaration .then user_check.php will

<?php
$var="1021";
include('user.html');
?>

and user.html will

<div id="var" type="hidden"><?php echo $var; ?></div>
Sign up to request clarification or add additional context in comments.

Comments

0

I assume you're looking for something like this:

<? php
$var="1021";
?> 

<input id="var" type="hidden" value="<?php echo $var; ?>"/>

You can then access the value in Javascript. For example, using jQuery:

$('#var').val();

2 Comments

how this will work ??can you explain ? both pages are different . one is user_check.php other is user.html
My mistake. You'd have to either to an ajax request or store the answer in localStorage.

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.