1

I have a javascript function to count the number of clicks. I wish to post the variable storing the count to PHP. I'm unsure why when I submit the form, the count is not echoed out. Below is the HTML file.

<html> 
<head>
<title> js php </title>
<script>
    var cnt=0;
    function CountFun(){
    cnt=parseInt(cnt)+parseInt(1);
    var divData=document.getElementById("showCount");
    divData.innerHTML="Number of Downloads: ("+cnt +")";//this part has been edited

    }
</script>
</head>
<body>

<div id="showCount"></div>
<form action "submitClicks.php"id="form">
    <input type="hidden" name="numberOfClicks" />
    <input type="button" id="btnClick" value="Click me" onclick="CountFun()"/>
    <input type="submit" value="Submit Clicks" />
</form>

</body>
</html>

Then this is my submit clicks page:

<?php
    $number_of_clicks = $_POST["cnt"];
    echo $number_of_clicks;
?>

Does this implementation look correct to you as when I click submit clicks, nothing happens. I'm expecting the number of clicks to be echoed.

2
  • what does "nothing happens" mean? do you see anything? is it everytime zero or what? Commented Apr 12, 2015 at 22:58
  • @t_01 when i click "click me" the number is counted and displayed. When i submit the clicks, the url changed from localhost/clicks/count.html to localhost/clicks/count.html?numberOfClicks= but the echo with the number of clicks is not displayed. Commented Apr 12, 2015 at 23:00

2 Answers 2

1
<form action "submitClicks.php"id="form">

Should be:

<form action="submitClicks.php" id="form" method="post">

And shouldn't you have something like:

In HTML:

<input id="count" type="hidden" name="cnt" value="">

In your Javascript CountFun function:

document.getElementById('count').value = cnt;
Sign up to request clarification or add additional context in comments.

5 Comments

This simply removes the data from the URL, which isn't really an issue. I'm trying to have the cnt javascript variable echoed in the PHP file.
I edited my post. At no point in your code do you actually add the value of the count to be submitted by the form.
@joemartin if you don't specify that you want to use post, you can't use _POST in your php.
Hm. With these changes to the code, nothing changes to the output. I still get a counter when I click the "click me" button but when I click "submit clicks", the count isn't echod in the PHP file.
I edited my answer, it should work now. If it doesn't work, please edit your post with your updated code so we can look at it again.
0

Try this code:

<html> 
<head>
<title> js php </title>
    <script>
        var cnt=0;
        function CountFun(){
        cnt=parseInt(cnt)+parseInt(1);
        var divData=document.getElementById("showCount");
        divData.value=cnt;//this part has been edited
        document.getElementById("JustShow").innerHTML= cnt;
        }
    </script>
</head>
<body>


<div id="JustShow"></div>
<form action="submitClicks.php" id="form" method="post">
    <input type="hidden" id="showCount" name="cnt" value="0" />
    <input type="button" id="btnClick" value="Click me" onclick="CountFun()"/>
    <input type="submit" value="Submit Clicks" />
</form>

</body>
</html>

2 Comments

This puts the cnt in the URL bar when I submit the clicks but the PHP gives this error : $number_of_clicks = $_POST["cnt"]; instead of the echo.
Just change form tag to : <form action="submitClicks.php" id="form" method="post">

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.