javascript - Failure to display result of a php script calculation in the textarea box -
i believe problem may lie in way trying initialize script. submit button using seems clear form, , not display results. think there may problem 2 lines " $message = $_post['textarea'];" , "echo $result + $message;" not sure if correct way produce result text area box, display miles driven , total cost in separate text boxes. want 1 button 3 tasks. there way efficiently php?
edit: corrected version of code. thank @fred-ii-!
<?php if(isset($_post['submit'])) { $x = $_post['beginningodometerreading']; $y = $_post['endingodometerreading']; $z = $_post['daysrentedcar']; $miles = $y - $x; $result = (15 * $z) + ($miles * 0.12); $message = $_post['textarea']; echo $result + $message; } ?> <html> <head> </script> </head> <body> <div align="center"> <hr><br> <form action="index4.php" method="post" name id="main"> <input type="text" id="name" name="customername" placeholder="enter name here" size="30px"> <br><br> <input type="text" id="address" name="customeraddress" placeholder="enter street address here" size="50px"> <br><br> <input type="text" id="city" name="customercity" placeholder="what city live in?" size="30px"> <br><br> <input type="number" id="zip" name="customerzip" placeholder="enter zip code" size="30px"> <br><br> <input type="number" id="bodometer" name="beginningodometerreading" placeholder="start odometer reading" size="80px"> <br><br> <input type="number" id="eodometer" name="endingodometerreading" placeholder="end odometer reading" width="80px"> <br><br> <input type="number" id="daysrented" name="daysrentedcar" placeholder="days rented" size="50px"> <br><br> <input type="submit" id="submit" value="submit"/> <br><br> miles driven: <input type="number" id='miles' min="1" max"10000" readonly="" /> total cost: <input type="number" id='result' min="1" max"10000" readonly="" /> <br><br> summary: <textarea cols="30" rows="2" name="thetextarea" id="textarea"> </textarea> <br><br> <input type="reset" value="reset"> </form> <hr> </div> </body>
there few things wrong here.
one of problems being conditional statement:
if(isset($_post['submit'])) {
it looking named element called "submit" submit button not named.
<input type="submit" id="submit" value="submit"/>
name it:
<input type="submit" name="submit" id="submit" value="submit"/>
and might have been relying on "id" it. in instance, can't.
that why you're getting blank page.
having used else{ echo "error"; }
have echo'd "error".
for example:
<?php if(isset($_post['submit'])) { $x = $_post['beginningodometerreading']; $y = $_post['endingodometerreading']; $z = $_post['daysrentedcar']; $miles = $y - $x; $result = (15 * $z) + ($miles * 0.12); $message = $_post['textarea']; echo $result + $message; } else { echo "error"; } ?>
then have
$message = $_post['textarea'];
but textarea named name="thetextarea"
.
- both of need match in names.
you using wrong concatenate operator +
for
echo $result + $message;
that needs dot. +
sign js/c++ method of concatenation, if 1 of variables contains string.
echo $result . $message;
or
echo $result . " ". $message;
had been mathematical equation 2 variables, yes; have been valid operator, not in case, since trying echo $message
variable, "textarea", being "text" , not integer.
sidenote: ensure indeed passing integer post array, add (int)
it. example: $x = (int)$_post['beginningodometerreading'];
edit: found few more errors in form.
you have 2x instances of max"10000"
it's missing equal sign max="10000"
and might missing name attributes both of these (which outlined missing =
signs max"10000"
.
miles driven: <input type="number" id='miles' min="1" max"10000" readonly="" /> total cost: <input type="number" id='result' min="1" max"10000" readonly="" />
if you're relying on js tagged as, please edit question , add code. however, there no code in php support this.
you've syntax error in
<form>
beingname id="main"
you need adjust actual code accordingly.
rewrite:
<?php if(isset($_post['submit'])) { $x = $_post['beginningodometerreading']; $y = $_post['endingodometerreading']; $z = $_post['daysrentedcar']; $miles = $y - $x; $result = (15 * $z) + ($miles * 0.12); $message = $_post['thetextarea']; echo $result . " " . $message; } else { echo "error"; } ?>
error reporting have signaled undefined index notice both of those.
add error reporting top of file(s) find errors.
<?php error_reporting(e_all); ini_set('display_errors', 1); // rest of code
sidenote: error reporting should done in staging, , never production.
Comments
Post a Comment