1

I am using php_self to submit a form. Once the data has been posted, I want to pass a calculated value to another form field on the same page, original form.

The $title_insurance field stays blank. Any ideas on why? Thanks!

<?php
if(isset($_POST['submit']))
{
$sale_price = $_POST['sale_price']; // posted value
$title_insurance = ($sale_price * 0.00575) + 200;

?>
<script type="text/javascript">
document.getElementById("title_insurance").value='<?php echo $title_insurance ; ?>'; 
</script>
<?php     }     ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="post" enctype="multipart/form-data">
 <input name="sale_price" type="text" id="sale_price" size="15">
<input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
<input name="submit" type="submit" class="bordered" id="submit" value="Calculate" />    
</form>
WGS
  • 199
  • 4
  • 17
  • You are checking isset($_POST['submit']) but you don't have a post variable named 'submit' in your form. You have a submit button, but it's not called 'submit' – jszobody Aug 16 '13 at 19:20
  • I just changed the name and id of the submit button to 'submit', but it still does not work. Is using JS the way to go or should php be populated the form's value? – WGS Aug 16 '13 at 19:24
  • A couple of tips for future reference If you are posting the form to the same page then you do not need the action attribute as this is the default behaviour. The enctype that you have use is also the default. The javascript routine will cause an error because the page hasn't been fully loaded. –  Aug 16 '13 at 19:25
  • @user2413654 See VIVEK-MDU answer below, his code works. And you don't need the javascript stuff at all. – jszobody Aug 16 '13 at 19:27
  • Don't test for a POST by looking for form fields. It's unreliable, as your code perfectly demonstrates. Use `if ($_SERVER["REQUEST_METHOD"] == 'POST']) { ... }` instead, which is 100% reliable. – Marc B Aug 16 '13 at 19:29
  • is unsafe against cross side scripting. if you want to post to the same page you should keep the action of form empty. – Raymond Nijland Aug 16 '13 at 19:31

4 Answers4

0

What you want to do is best done by AJAX. The <form> construction is outdated and not useful unless you are transferring the user to another page and sending some data along with it - or, if you are finished getting user data and just want to process what was entered and display a completion message.

If you wish to continue processing on the same page, then AJAX is the way to go. And the best way to use AJAX is to have a separate processor file (PHP) that receives the data, processes it, and sends it back.

To convert a <form> construct to AJAX, you really just need to remove the <form></form> tags and convert the submit button from type="submit" to type="button" id="mybutton", and use the IDs on the button and on the other elements to grab the data they contain and feed them to the AJAX code block. The examples in the link at bottom shows what you need to know - they are simple, helpful examples.

To conserve resources, you can use the same PHP processor page for multiple AJAX requests -- just send a variable (eg. 'request=save_to_db&first_name=bob&last_name=jones', ) and test for what "request" is received, that will determine what your PHP processor file does and echoes back.

This post, and the examples it contains, will help.

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
0

try this first

In you coding you missed this $_POST['button']

and

<?php
if(isset($_POST['button']))
{
$sale_price = $_POST['sale_price']; // posted value
$title_insurance = ($sale_price * 0.00575) + 200;

?>
<script type="text/javascript">
document.getElementById("title_insurance ").value='<?php echo $title_insurance ; ?>'; 
</script>
<?php     }     ?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="post" enctype="multipart/form-data">
 <input name="sale_price" type="text" id="sale_price" size="15">
<input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
<input name="button" type="submit" class="bordered" id="button" value="Calculate" />    
</form>

and also refer this FIDDLE it will more helpful to you..

VIVEK-MDU
  • 2,483
  • 3
  • 36
  • 63
0

You have an extra space in your getElementById parameter:

//                                      VV
document.getElementById("title_insurance ").value='<?php echo $title_insurance ; ?>'; 
losnir
  • 1,141
  • 9
  • 14
0

The submit button is called button, also if you are outputting a javascript to amend the value it need to be run after the DOM has created the element title_insurance.

if(isset($_POST['button']))
{
    $sale_price = $_POST['sale_price']; // posted value
    $title_insurance = ($sale_price * 0.00575) + 200;
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="post" enctype="multipart/form-data">
   <input name="sale_price" type="text" id="sale_price" size="15">
   <input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
   <input name="button" type="submit" class="bordered" id="button" value="Calculate" />    
</form>


<script type="text/javascript">
    document.getElementById("title_insurance").value='<?php echo $title_insurance ; ?>'; 
</script>

A better way in this case would be to forget about the javascript as it is unnecessary and do this

// I am assuming you have initialized $title_insurance 
// somewhere above here to its default value!!!!
$title_insurance = isset($_POST['button']) ? ($_POST['sale_price'] * 0.00575) + 200 : $title_insurance;
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="post" enctype="multipart/form-data">
   <input name="sale_price" type="text" id="sale_price" size="15">
   <input name="title_insurance" type="text" id="title_insurance" size="15" value="<?php echo $title_insurance; ?>" />
   <input name="button" type="submit" class="bordered" id="button" value="Calculate" />    
</form>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • The method described in the second section (no JS) worked perfectly! Thanks so much for your help! – WGS Aug 16 '13 at 19:53