1

I am sending multiple values from the input field with the same name. It works fine when I just send the custom1 and var 2 but I want to add a javascript variable var3 as well but assigning the value by using document.getElemendById overwrites the input field completely and custom1 and var2 are lost. So how to add a javascript variable var3 at the end so it gets printed under the array index $pieces[2]; in the v.php file.

 <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <?php $var2 = "custom2" ?>
    <form action="v.php" method="POST" >
    <input id="jsvar" type="text" name="custom" value="custom1,<?php echo $var2; ?>">
    <input type="submit" name="" value="send">
    </form>
    <script type="text/javascript">
            var var3 = "custom3";
            document.getElementById("jsvar").value = var3;
        </script>
    </body>
    </html>

v.php

    <?php  
    $custom = $_POST['custom'];
    $pattern = ",";
    $pieces = explode($pattern,$custom, 3);
    print_r($pieces);
    $custom1 = $pieces[0];
    $custom2 = $pieces[1];
    echo '<br>';
    echo $custom1.'<br>';
    echo $custom2.'<br>';
    echo $pieces[2];
    ?>
2
  • Where is jsvar in your form? Commented Dec 24, 2018 at 7:17
  • its there in the first file Commented Dec 24, 2018 at 7:20

2 Answers 2

3

You could append the data rather than replace it with the new data.

var var3 = "custom3";
document.getElementById("jsvar").value += ","+var3;
Sign up to request clarification or add additional context in comments.

Comments

2

Try this below . It's working fine. Review below screen Link

<!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <?php $var2 = "custom2" ?>
    <form action="v.php" method="POST" >
    <input id="jsvar" type="text" name="custom" value="custom1,<?php echo $var2; ?>">
    <input type="submit" name="" value="send">
    </form>
    <script type="text/javascript">
            var pre_value=document.getElementById("jsvar").value;
            var var3 = "custom3";
            var new_value = pre_value +','+var3;
            document.getElementById("jsvar").value = new_value;
        </script>
    </body>
    </html>

1 Comment

Yea thanks, it works and appending with += also does the same. document.getElementById("jsvar").value += ","+var3;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.