Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP and (isset($_POST[''])) issue

Tags:

php

Apologies if this question is duplicated, I have however not found an obvious answer to my question. I am a complete novice with php, be kind! When I load the following code, it appears to run if(isset($_POST['test'])) even though I have not pressed the button "test". I only want the code to execute when I press the "test" button. How do I resolve this?

<?php
    if(isset($_POST['update'])) {
        $content=simplexml_load_file("books.xml");
        $value=$content->book->price;
        $sum=$content->book->price;
    }

    if(isset($_POST['test'])) {
        $content=simplexml_load_file("books.xml");
        $value=$content->book->price;
        $content->book->price = $value + 1.0;
        $content->asXML("books.xml");
        $sum=$content->book->price;
    }
?>

<body>
    <form method="post">
        <input type="checkbox" id="i1check1" onchange="toggleDisabled(this.checked)"> Alarm 01
        <input type="text" id="i1text1" name="i1" size="80" maxlength="128" value="<?php echo @$sum;?>"/>
        <input type="submit" id="i1btn1" name="update" value="Update"/>
        <input type="submit" id="i1btn2" name="test" value="Test"/>
    </form>

    <script>
        function toggleDisabled(_checked) {
            document.getElementById('i1text1').disabled = _checked ? false : true;
            document.getElementById('i1btn1').disabled = _checked ? false : true;
        }
    </script>
</body>
like image 253
Juan Le Roux Avatar asked Jun 01 '16 05:06

Juan Le Roux


1 Answers

Please Try This

if(isset($_POST['update']) && $_POST['update'] == "Update"){
    $content=simplexml_load_file("books.xml");
    $value=$content->book->price;
    $sum=$content->book->price;
}

if(isset($_POST['test']) && $_POST['test'] == "Test"){
    $content=simplexml_load_file("books.xml");
    $value=$content->book->price;
    $content->book->price = $value + 1.0;
    $content->asXML("books.xml");
    $sum=$content->book->price;
}
like image 87
Manish Jesani Avatar answered Sep 27 '22 21:09

Manish Jesani