0

I try to create a button in php and increase or decrease its value (inside the text input) on click.

<?php

echo "<script>
    function inc(elem)
    {
        x = elem.value;
        //alert('dsadasdasdsadasdas');
        if(x<31)
        {
        x= x+1;
        }
        alert(x);
        elem.value = x;
    }
    </script>";

            echo '<form action="tziros.php" method="post">';
            echo '<input type="text" value="1" name="tziros_imeras">';
            echo '<br>';
            //echo '<input type="button" value="ADD +" onClick="inc(document.getElementById("tziros_imeras"))">';
            echo '<input type="button" value="ADD +" onClick="inc(document.getElementById(\'tziros_imeras\'))">'
            //echo '<input type="button" value="DEC -" onClick="dec();">';  
            echo '<input type="submit" name="submit_tziros_meras" value="OK">';
            echo '</form>'; 
?>

Problem is that javascript is not running at all .

EDIT : after reading your answers i came up with this: js:

 function inc(elem)
        {
        elem.value++;
        }

and on the form:

echo '<input type="text" value="1" id="tziros_imeras" name="tziros_imeras">';

So now at last, the js is running .

3
  • Why are you putting that all within PHP? Commented Aug 12, 2013 at 18:08
  • 1
    Stylistic note: If you output mutliple lines of HTML or Javascript, break out of ?> PHP mode, or use a HEREDOC string. Commented Aug 12, 2013 at 18:08
  • i can't see element with the specified id Commented Aug 12, 2013 at 18:11

3 Answers 3

1
  • You're looking for tziros_imeras by ID (getElementByID) but you have only the name property set up to tziros_imeras

  • you have only the method inc() - you're missing the method dec()

and most important:

  • you're incrementing x which is a local variable (it doesn't affect the actual element), you should do instead:

    elem.value += 1;

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

2 Comments

i know it try to fix the inc first
@ΒασιληςΙωσηφιδης check out the last bullet!
0

Update your PHP to use this form so it is easier to read and debug:

echo reference

echo <<<END
content here will be printed
multiple lines!
END;

I'm not sure I believe you, that is is not "running". Insert an alert in your JavaScript to verify.

alert('I am alive');

2 Comments

what does it have with the question?
Nothing directly, but it does make it easier to read (though dropping out of PHP completely would be even easier): so it should be a comment rather than an answer
0

This part of the code:

echo '<input type="button" value="ADD +" onClick="inc(document.getElementById("tziros_imeras"))">';

Needs to be like this:

echo '<input type="button" value="ADD +" onClick="inc(document.getElementById(\'tziros_imeras\'))">';

The JS being outputted was like this:

onClick="inc(document.getElementById("tziros_imeras"))"

Which, as you'll notice, has messed up quotes. This is what was causing the JS to fail to run (look in your browser console and you'll see the errors as well).

As some others have said, you have some JS errors as well, but this is the main issue related to your question.

2 Comments

i switched the line with the one of yours but still the alerts i have dont run.
Note that I said you still have some JS errors. Your other problem is that you're trying to use document.getElementById, but tziros_imeras is the NAME of the text field.

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.