logo
Apache Lounge
Webmasters

 


About

Forum Index Downloads Search Register Log in  RSS Apache Lounge
 



Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Apache Lounge is not sponsored.

Your donations will help to keep this site alive and well, and continuing building binaries.



getting javascript result to php

 
Post new topic   Reply to topic    Apache Forum Index -> Coding & Scripting Corner



View previous topic :: View next topic  
Author Message
Ron Ford



Joined: 16 Feb 2017
Posts: 5
Location: Canada, Vancouver

PostPosted: Mon 27 Feb '17 23:32    Post subject: getting javascript result to php Reply with quote

I want to get the length of string in pixels, load it into a hidden form variable and automatically submit the form. It's not doing what I want. I'd be grateful for any insight. Thanks in advance.

<?php
if (isset($_POST['result'])) {
print($_POST['result']);
die(); // just to show it's working
}
?>

<!DOCTYPE html>
<html>
<?php require("includes/header.php"); ?>
<script type="text/javascript">
function submitform() {
var test = document.getElementById("Test");
var width = (test.clientWidth + 1) + "px";
document.getElementById("size").innerHTML = width;
document.forms["myForm"].submit();
}
<body onload="submitform()">
<form id="myForm" action="strsize.php" method="post">
<div id="Test" style="position:absolute;white-space:nowrap">
abcdefghijklmnopqrstuvwxyz
</div>
<input id="size" type="visiblity:hidden" name="result" />
</form>
</div>
</body>
</html>
Back to top
mraddi



Joined: 27 Jun 2016
Posts: 28
Location: Schömberg, Baden-Württemberg, Germany

PostPosted: Thu 02 Mar '17 17:13    Post subject: Reply with quote

use .value not .innerHTML as you want to change the value of the input-field - see https://www.w3schools.com/jsref/prop_text_value.asp

Code:
<?php
if (isset($_POST['result'])) {
  print($_POST['result']);
  die(); // just to show it's working
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script type="text/javascript">
  function submitform() {
    var test = document.getElementById("Test");
    var width = (test.clientWidth + 1) + "px";
    document.getElementById("size").value = width;
    document.forms["myForm"].submit();
  }
</script>
</head>

<body onLoad="submitform()">
  <div id="Test" style="position:absolute;white-space:nowrap">
    abcdefghijklmnopqrstuvwxyz
  </div>
  <form id="myForm" action="strsize.php" method="post">
    <input id="size" type="hidden" name="result" />
  </form>
</body>
</html>
Back to top
Ron Ford



Joined: 16 Feb 2017
Posts: 5
Location: Canada, Vancouver

PostPosted: Sat 04 Mar '17 23:16    Post subject: Reply with quote

Thank you, just what was needed.
Back to top


Post new topic   Reply to topic    Apache Forum Index -> Coding & Scripting Corner
Page 1 of 1