I took data from form then put that in Javascript variable then add it and put it back in php variable to print it. I am learning php and wanted to use javascript and php together.
<?php
if(isset($_GET['submit'])){
$firstName = $_GET['firstValue'];
$secondName = $_GET['secondValue'];
echo"
<script>
let firstValue = $firstName,
secondValue = $secondName;
$result = firstValue + secondValue;
document.getElementById('result').value = $result;
</script>
";
}
?>
<form method="get">
<input type="number" name="firstValue">
<input type='number' name='secondValue'>
<input type = 'number' id='result'/> /* I want to put result in this input tag. */
<input type="submit" value="click" name='submit'> /*Submit button*/
</form>
I was getting error that result variable undefined but why??
To remove error i removed $result variable and try putting value directly in input tag.
<?php
if(isset($_GET['submit'])){
$firstName = $_GET['firstValue'];
$secondName = $_GET['secondValue'];
echo"
<script>
let firstValue = $firstName,
secondValue = $secondName;
document.getElementById('result').value = firstValue + secondValue;
</script>
";
}
?>
<form method="get">
<input type="number" name="firstValue">
<input type='number' name='secondValue'>
<input type = 'number' id='result'/> /* I want to put result in this input tag. */
<input type="submit" value="click" name='submit'> /*Submit button*/
</form>
Now no error but code is still not working.