logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


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

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: getting javascript result to php
Author
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: 149
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


Reply to topic   Topic: getting javascript result to php View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner