0

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.

  • you could use AJAX – Jaromanda X Aug 01 '23 at 04:04
  • 1
    It seems the `` value could be populated directly by PHP. Is there a reason you're using JavaScript? – showdev Aug 01 '23 at 04:07
  • 1
    You never assigned the PHP variable `$result`. That should be a JavaScript variable. – Barmar Aug 01 '23 at 04:10
  • 1
    The problem in the second version is that the JavaScript is before the HTML elements, so `document.getElementById('result')` doesn't find the element. Aren't you getting an error in the browser console? – Barmar Aug 01 '23 at 04:12
  • I know we can but i wanted to learn if javascript and php can work together @showdev – Anand Choudhary Aug 01 '23 at 04:12
  • 1
    I certainly hope that when you're trying to debug JavaScript problems you have Developer Tools open. – Barmar Aug 01 '23 at 04:13
  • Thank you @Barmar the problem is solved actually i was using replit rather than running xampp because it crashes a lot. second thing is that replit was refreshing answer again and again after pressing submit button. I am not going to use it from now. – Anand Choudhary Aug 01 '23 at 04:24
  • XAMPP doesn't crash a lot. Probably there's some issue with your install. Try re-installing from scratch – ADyson Aug 01 '23 at 08:11
  • @ADyson if i mistakenly turn of my pc when xampp is running then it stop working and give error "Unexpected shutdown". Something like that. I lost my complete database 3 times. – Anand Choudhary Aug 02 '23 at 07:59

0 Answers0