admin管理员组文章数量:1406942
Forgive me for asking a seemingly obvious question, but all of my searching is turning up guides on how to create POST values with JS, not how to grab (and utilize) them.
What I want to do:
step1.php -- form POST
ing to step2.php
step2.php -- also a form, JS grabs one particular POST value and does some work with it, then updates this new form accordingly
for reasons not worth getting into, the process needs to be a 2 step//2 page process.
The obvious solution is just to do something like:
<script type="text/javascript">
function damn_ugly () {
var shameful = <?php echo $_POST['desiredDatum']; ?>;
do more stuff...
}
</script>
but that seems a bit dirty to me.
Is there a better way? or is that really how I'm supposed to do it?
Forgive me for asking a seemingly obvious question, but all of my searching is turning up guides on how to create POST values with JS, not how to grab (and utilize) them.
What I want to do:
step1.php -- form POST
ing to step2.php
step2.php -- also a form, JS grabs one particular POST value and does some work with it, then updates this new form accordingly
for reasons not worth getting into, the process needs to be a 2 step//2 page process.
The obvious solution is just to do something like:
<script type="text/javascript">
function damn_ugly () {
var shameful = <?php echo $_POST['desiredDatum']; ?>;
do more stuff...
}
</script>
but that seems a bit dirty to me.
Is there a better way? or is that really how I'm supposed to do it?
Share Improve this question asked Apr 11, 2011 at 13:02 DrewDrew 6,28410 gold badges48 silver badges70 bronze badges 3- any particular reason why you cannot make the first step1.php page be dynamic and do an ajax post to step2.php, return some data that some javascript function can use? This breaks you '2step/2page process' but seems to be a better solution if you're not liking the formatting as above... – g19fanatic Commented Apr 11, 2011 at 13:11
- To be honest, unifying the functionality of those 2 forms into one is simply beyond my present ability, and I'm the sole coder on the site in question. I'm only just taking my first hesitant steps into adding incremental javascript enhancements to a strong PHP foundation, and am trying very hard not to bite off more than I can chew. :-) – Drew Commented Apr 11, 2011 at 13:14
- take a look at jQuery and some of their examples... You will find that it is MUCH easier than you suspect.. one call and it sends your data (which was packed into a JS array) to your PHP form (which you already know how to use) and it then returns to a JS function whatever data you've set to output from the php form. docs.jquery./Main_Page – g19fanatic Commented Apr 11, 2011 at 13:20
5 Answers
Reset to default 4var postData = <?php echo json_encode($_POST); ?>;
You can obviously change that to include only certain fields from $_POST
by passing a custom array to json_encode
.
var desiredDatum = <?php echo json_encode($_POST['desiredDatum']); ?>;
There is no nicer way to do it - and using json_encode
ensures no matter what's contained in the POST variable nothing will break (at least not during the assignment).
That is how you are supposed to do it, JavaScript cannot access the POST values in another way.
Offcourse you can make it a bit more beautiful: have your php-script put the POST variables in an array, and print the array in JSON format. Now your javascript has the array.
POST values are being sent to the server. Once submitted only the server can work with those values. Your example is pretty much the only option you have to "access" POST values that have been sent to the server in the previous request.
JS has no access to POST values, it can only retrieve GET values. However, since you're using a PHP script and you're able to pass the data to JS - why not give your JS functions data in JSON format? You can use PHP's json_encode function to encode all POST values to JSON that you can use then easily in your JS code.
Try this On second step Page 2:
Set hidden text box on page:
<input type='hidden' name='text1' id = 'text1' value='<?php echo $_POST['desiredDatum']; ?>' />
Now if you use jQuery the wirite below given code
$(document).ready(function() {
var shameful = $('#text1').val();
});
if not using jQuery Then
var shameful = document.getElementById('text1').value;
I hope this is what you are looking for.
本文标签: phpgrabbing POST values with javascriptStack Overflow
版权声明:本文标题:php - grabbing POST values with javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744943466a2633633.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论