vhdz04
vhdz04

Reputation: 159

How can I pass multiple variables to a JavaScript function in HTML / PHP?

I need to call a javascript function from php through embedded html.

This is my function.

<script> 
    // THIS JAVASCRIPT REDIRECTS TO EDITUSER.PHP AND PASSES USERID VALUE.
    function startMaint(mo, yr){        
        window.location = "startmaintenance.php?mo=" + mo + "yr=" + yr;
    }   
</script>

this is where i call my function.

<form method="POST" action="home.php">

                <select  name="monthselect">

                    <option value="" disabled selected> Select Month: </option>

                    <?PHP
                        echo"<option value=01>January</option>";
                        echo"<option value=02>February</option>";
                        echo"<option value=03>March</option>";
                        echo"<option value=04>April</option>";
                        echo"<option value=05>May</option>";
                        echo"<option value=06>June</option>";
                        echo"<option value=07>July</option>";
                        echo"<option value=08>August</option>";
                        echo"<option value=09>September</option>";
                        echo"<option value=10>October</option>";
                        echo"<option value=11>November</option>";
                        echo"<option value=12>December</option>";

                    ?>

                </select>&nbsp;&nbsp;&nbsp;

                <select  name="yearselect">

                    <option value="" disabled selected> Select Year: </option>

                    <?PHP

                        $year = 1920;

                        While($year <= date("Y")){
                            echo"<option value=$year>".$year."</option>";
                            $year++;
                        }


                echo"</select>";


            echo "</div>";

            echo "<div class='center'>";

                    $strMonth = $_POST['monthselect'];
                    $strYear = $_POST['yearselect'];

                    echo "<input type='button' class='btn' name='start' value='Start' onclick='javascript:startMaint('".$strMonth."','".$strYear."');'>";

                ?>

                </form>

The issue that I am having is that my code is not executing when i press the 'Start' button.

I am not sure if the syntax that I am using is correct when i pass the arguments to the function. any help would be appreciated.

if i manually input the values like this:

echo "<input type='button' class='btn' name='start' value='Start' onclick='javascript:startMaint(". 1 .",". 1980 .");'>";

It works fine. but if i replace the values with variables, the code does not execute...what i want to do is pass the $strMonth and $strYear values to the function.

Upvotes: 1

Views: 1907

Answers (3)

Achyut Krishna Deka
Achyut Krishna Deka

Reputation: 759

php is not a even driven programming language and it's a server side programming language so you need to solve your problem using javascript

Solve the issue this way

<script>
 document.getElementById('btn').addEventListener("click",  function(){
  var mo =   document.getElementById('monthselect').value;
  var yr = document.getElementById('yearselect').value;
  window.location = "startmaintenance.php?mo="+mo+"yr="+yr;
});
</script>

Html & PHP code

<form method="POST" action="home.php">

                <select id="monthselect"  name="monthselect">

                    <option value="" disabled selected> Select Month: </option>

                    <?php
                        echo"<option value=01>January</option>";
                        echo"<option value=02>February</option>";
                        echo"<option value=03>March</option>";
                        echo"<option value=04>April</option>";
                        echo"<option value=05>May</option>";
                        echo"<option value=06>June</option>";
                        echo"<option value=07>July</option>";
                        echo"<option value=08>August</option>";
                        echo"<option value=09>September</option>";
                        echo"<option value=10>October</option>";
                        echo"<option value=11>November</option>";
                        echo"<option value=12>December</option>";

                    ?>

                </select>&nbsp;&nbsp;&nbsp;

                <select id="yearselect"  name="yearselect">

                    <option value="" disabled selected> Select Year: </option>

                    <?php

                        $year = 1920;

                        While($year<= date("Y")){
                            echo"<option value=$year>".$year."</option>";
                            $year++;
                        }
                    ?>

              </select>


            </div>

            <div class='center'>

  <input type='button' id="btn" class='btn' name='start' value='Start' >

                </form>

In the Html code i have added three id attribute id="monthselect", id="yearselect", id="btn"

Upvotes: 1

prestige Godson
prestige Godson

Reputation: 31

        function startMaint(mo, yr){

            window.location.href = "startmaintenance.php?mo="+mo+"& yr="yr;

        }

You use ampersand symbol to concatenate parameters being passed via Get method, and you need the href property of the location object for redirection

Upvotes: 0

funwhilelost
funwhilelost

Reputation: 2010

You'll need to assign window.location.href. Multiple parameters are separated by an ampersand "&". You also forgot a "+" in your string concatenation.

window.location.href = "startmaintenance.php?mo=" + mo + "&yr=" + yr;

Upvotes: 0

Related Questions