0

The value of the button should change in 1 or 0, but the echo $_POST["ordina"] give always 1 and I don't understand because the code seems correct.

<script> function order() {
if (document.ordination.ordina.value == "1") { 
    document.ordination.ordina.value = "0";
} else { 
    document.ordination.ordina.value = "1"; 
} }</script>

<?php echo $_POST["ordina"]; ?>

<form id="ordination" name="ordination" method="POST" action="">
<button type="submit" value="1" class="button" name="ordina" onclick="order();return true;">Ordina</button>

The alert(document.ordination.ordina.value) give always 1.

Some can help me?

4
  • It's actually changing in my code. Commented Mar 28, 2013 at 12:26
  • alert(document.ordination.ordina.value) first to see if you are accessing the right thing :) Commented Mar 28, 2013 at 12:26
  • @David Just for a bit of fun, the same code shortened Commented Mar 28, 2013 at 12:35
  • Every time you submit the form, the page is loading, so the if condition will never be true and the value will always be set to 1. Commented Mar 28, 2013 at 12:37

3 Answers 3

1

Check it now..

<script> function order() {
if (document.ordination.ordina.value == "1") {
     alert(document.ordination.ordina.value);  // this one shows 1
    document.ordination.ordina.value = "0";
} else { 
    alert(document.ordination.ordina.value);  // this one shows 0
    document.ordination.ordina.value = "1"; 
} }</script>

<?php echo $_POST['ordina'];?>

<form id="ordination" name="ordination" method="POST" action="">
<button type="submit" value="<?php if(isset($_POST['ordina'])){echo $_POST['ordina'];}else{ echo '1';}?>" class="button" name="ordina" id="ordina" onclick="order();return true;">Ordina</button>

after you submit this form the <?php echo $_POST['ordina'];?> is 0... and set to button value as 0 and again submit the value can be changed to 1.

and its consequently changed 0 to 1 to 0 to 1 but you have first time load this page means the alert shows 1 only..

Sign up to request clarification or add additional context in comments.

3 Comments

How does this answer the question? Or even solve the problem?
They need only the alert=>alert(document.ordination.ordina.value) . first alert shows '1' and second alert shows '0' because the value changed by the script document.ordination.ordina.value = "0";. and submitting the value returned by the echo $_POST['ordina'] is 0 only.. pls check this in your environment..
now i have edited by the value in button element => value="<?php if(isset($_POST['ordina'])){echo $_POST['ordina'];}else{ echo '1';}?>"
1

This always return 1, because your button is a "submit" button, so the body is reloaded each time you click on the button.

    <script>
    function order() {
    if (document.ordination.ordina.value == "1") { 
        document.ordination.ordina.value = "0";
    } else { 
        document.ordination.ordina.value = "1"; 
    }
    alert(document.ordination.ordina.value);
    }
    </script>

    <form id="ordination" name="ordination" method="POST" action="">
    <button type="button" value="1" class="button" name="ordina" id="ordina" onclick="order();return true;">Ordina</button>

    </form>

I change type "submit" for "button", then it works.

Comments

0
<script> function order() {
if ($("#ordina").val() == "1") { 
    $("#ordina").val(0);
} else { 
    $("#ordina").val(1); 
} }</script>

<?php echo $_POST["ordina"]; ?>

<form id="ordination" name="ordination" method="POST" action="">
<button type="submit" value="1" class="button" name="ordina" id="ordina" onclick="order();return true;">Ordina</button>

2 Comments

What is va supposed to be?
$("#ordina").va() its not a correct stmt .. $("#ordina").val() is the right one.

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.