3

In the past, whenever I needed to get the value of an html element, I would always submit a form to load a different page. Something like:

page1.php

<form name="form1" action="page2.php" method="post">
   <input type="hidden" name="input1" value="value1" />
</form>

page2.php

<?php
   $data = $_POST['input1'];
?>

My question is, is it possible to get the value of 'input1' on the same page (page1.php) without requiring clicking a submit button?

2 Answers 2

4

With jQuery:

<?php 
    if (isset($_POST['data'])) { 
        $data = $_POST['data']; 
        print( "data is: $data" ); 
        return; 
    } 
?> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script type="text/javascript" src="jquery-1.6.3.js"></script> 
</head> 
<body> 

<div>Response from server: <span id="response"></span></div> 
<script type="text/javascript"> 
    $.post('test.php',{'data': "value1"}, function (data) {
        $('#response').text(data);
    }); 
</script> 
</body> 
</html>

Non-jQuery IE doesn't have the XMLHttpRequest object so here is a shim for IE:

Yes, using ajax / javascript:

var formData = new FormData();

formData.append("input1", "value1");

var xhr = new XMLHttpRequest();
xhr.open("POST", "page1.php");
xhr.send(formData);

if (typeof XMLHttpRequest == "undefined") XMLHttpRequest = function() {
    try {
        return new ActiveXObject("Msxml2.XMLHTTP.6.0");
    }
    catch (e) {}
    try {
        return new ActiveXObject("Msxml2.XMLHTTP.3.0");
    }
    catch (e) {}
    try {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {}
    //Microsoft.XMLHTTP points to Msxml2.XMLHTTP and is redundant
    throw new Error("This browser does not support XMLHttpRequest.");
};
Sign up to request clarification or add additional context in comments.

11 Comments

I like the jQuery solution, but I'm a little confused. Is that one line of code the only one needed? And where exactly does it go? How is the php variable assigned?
Yes, with jQuery you only need that one line. In your php, you would access it like $_POST['input1']
Hmmm, I must be doing something wrong. In the head of page1.php I have <script type="text/javascript" src="jquery-1.6.3.js"></script> Then in the body I have <form method="post" action="" name="form2">\n<input type="hidden" name="timezone" value="hi" />\n</form>\n<script type="text/javascript">$.post('page1.php', {input1: 'value1'});\n</script>\n<?php\n$data = $_POST['input1'];\nprint( "value is: $data" );\n?> I'm getting a blank ($data is empty).
'timezone' and 'hi' are supposed to be 'input1' and 'value1'
Man, I just can't get it to work. I've simplified it as much as possible. Here's the entire page. <!DOCTYPE...> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="jquery-1.6.3.js"></script> </head> <body> <script type="text/javascript"> $.post('test.php',{'data': "value1"}); </script> <?php $data = $_POST['data']; print( "data is: $data" ); ?> </body> </html> If you need to, I can tell you the url where it's hosted.
|
2

What for? Do you want to submit it to another file without needing to actually "submit" the form?

You use JavaScript for that.

document.form1.input1.value

2 Comments

I wish those who vote down explain why they voted down. It's not healthy you know, voting down without any reason. =)
I think they voted you down, beacuse they expected you to bring an PHP'ish way.

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.