Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a little bit complicated situation with codes but I made an easy example so that It is easier for you to understand what I actually need.

HTML:

<html>
    <body>
        <input type="button" value="Increase" name="increase" id="increase"></input>
        <input type="button" value="Submit" name="submit" id="submit"></input>
    </body>
</html>

Javascript:

$(document).ready(function() {
    $count = 1;
    $('input').click(function() {
        $count++;
    });
});

And the idea of all this web-page:
1)Click few times on button "Increase";
2)The javascript variable $count increases each time You clicked on that button;
3)When You click button "Submit", it makes php loop that repeats $count times and each repeating time it makes new record in some database table. Here is the example of possible php for loop:

for($i=1; $i<=$count; $i++) {
    //Here makes new record in some database table. For example, it makes some tables rows "Number" = $i; <-- This is not important in my problem.
}

The question is about middle part of declaring for loop: "$i<=$count" - how it is possible to tell until what point I want this loop with variable, if that variable was made and increased in Javascript language? :?

share|improve this question
    
You want to send the count to the server? could you elaborate? –  Alex Shilman Jan 9 at 22:55
3  
store the changed count in a hidden form element (updated onclick) –  Dagon Jan 9 at 22:56
    
cant we store $count++ in a new variable then use this variable in you for loop? –  Lion Liu Jan 9 at 23:08
    
Isn't the count variable limited to the scope of the "ready()" function? Don't you have to declare it as global? –  Fabien Warniez Jan 9 at 23:09
    
@FabienWarniez Actually as in the code it is a global variable. Though you cannot use (should not use) $ as a JS variable name. But I refer to omitting the var in front of it! –  DOC ASAREL Jan 9 at 23:16

3 Answers 3

PHP interpratate HTML, so you are can send variable via ajax only!

share|improve this answer
    
If you want to leave the page you could also add it to the href. –  DOC ASAREL Jan 9 at 23:03
1  
You all right! But i am mean without leave page. –  Arthur Yakovlev Jan 9 at 23:05

You could add an input field to your form and each time you increase your count variable you update its value, so when submitted, you can extract that number via the request.

$(document).ready(function() {
    $count = 1;
    $('input').click(function() {
        $count++;
        $('#your-input-field').val($count);
    });
});

You should change your form like

<html>
    <body>
        <form method="post" action="/">
            <input type="text" value="0" id="your-input-field" name="count"></input>
            <input type="button" value="Increase" name="increase" id="increase"></input>
            <input type="button" value="Submit" name="submit" id="submit"></input>
        </form>
    </body>
</html>

now you can extrach that number via $_POST['count']

share|improve this answer

You can't use the javascript variable directly in the PHP.

I think the best way to achieve what you want to do is using an AJAX request. You should use the javascript to send a request to another URL and then use the variable.

This is how you should do:

var url = 'create_database_records.php';
var request = $.post(url, { count: count });
request.done(function(data) {
   // request is done
});

Notice that in this method you should have access to the count variable.

In the *create_database_records.php* you should use the $_POST['count'] variable.

Note: You should not use $ declaring javascript variables, use var count = 1 instead.

UPDATE: Another way to do this

You may want to put this buttons in a form, and add a new input (type: hidden) that will hold the counter.

<form action="create_database_records.php" method="POST">
    <input type="hidden" value="1" name="count" id="count" />
    <input type="button" value="Increase" name="increase" id="increase" />
    <input type="submit" value="Submit" name="submit" id="submit" />
</form>

Then, in the click event you should change the hidden field value:

$(document).ready(function() {
    count = 1;
    $('#increase').click(function() {
        count++;
        $('[name=count]').attr('value', count);
    });
});

In the *create_database_records.php* you should use the $_POST['count'] variable.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.