Ognjen Sekulovic
Ognjen Sekulovic

Reputation: 19

How to send a result to a form with php?

I'm really new to PHP but it's awesome language :)

I created simple mathematical calculation script that takes inputs from forms in terms of variables and perform simple calculations. I would like a result to be displayed in another form instead of simply being displayed at the bottom of the page.

<html>
<body>
<p>Simple PHP script for cloning calculations</p>
<form name="form1" method="post" action="">
<p>
<label for="vector_lenght">La taille du vecteur (kb)</label>
<input type="text" name="vector_lenght" id="vector_lenght">
</p>
<p>
<label for="insert_lenght">La taille de l'insert (kb)</label>
<input type="text" name="insert_lenght" id="insert_lenght">
</p>
<p>
<label for="conc_insert">La concentration de l'insert</label>
<input type="text" name="conc_insert" id="conc_insert">
</p>
<p>
<input type="submit" name="ratio_calc" id="ratio_calc" value="Calculer">
</p>
<p>
<label for="ratio">Le ratio à utiliser</label>
<input type="text" name="ratio" id="ratio">
</p>
<p>
<label for="ratio_1">ratio 1:1</label>
<input type="text" name="ratio_1" id="ratio_1">
<br>
<label for="ratio_3">ratio 1:3</label>
<input type="text" name="ratio_3" id="ratio_3">
<br>
<label for="ratio_5">ratio 1:5</label>
<input type="text" name="ratio_5" id="ratio_5">
</p>
</form>

<?php

if($_POST['ratio_calc']!=''){ 
define("conc_vector", 100);
$vector_lenght = $_POST['vector_lenght']; 
$insert_lenght = $_POST['insert_lenght'];
$conc_insert = $_POST['conc_insert'];
$result = (conc_vector * $insert_lenght) / $vector_lenght; 
echo "<h1>$result</h1>"; 
} 


?>

</body>
</html>

Upvotes: 1

Views: 204

Answers (1)

Fiarr
Fiarr

Reputation: 858

In your opening form tag, the 'action' attribute specifies what file should receive the HTTP request created when you submit the form. As it is now the value is an empty string, and the script is defaulting to POSTing the data back to the current script.

(I assume this other form is in a different page - if not, skip to the last code snippet)

So, create another PHP script that represents the page you want to contain your results, and put the name of the new script as the value of the 'action' attribute. In the new script, you can retrieve the values sent by your form via the $_POST[] superglobal (in the same way that you already are in the code you posted).

If the new page is named resultPage.php (and is in the same directory as the script you posted), your form tag should look like this:

<form name="form1" method="post" action="resultPage.php">

If you want the data to display inside a form in the new page, do something like this:

<input type="text" value="<?php echo $_POST['conc_insert'] ?>">

Upvotes: 3

Related Questions