1

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? :?

5
  • You want to send the count to the server? could you elaborate? Commented Jan 9, 2014 at 22:55
  • 3
    store the changed count in a hidden form element (updated onclick) Commented Jan 9, 2014 at 22:56
  • cant we store $count++ in a new variable then use this variable in you for loop? Commented Jan 9, 2014 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? Commented Jan 9, 2014 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! Commented Jan 9, 2014 at 23:16

3 Answers 3

0

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

1
  • If you want to leave the page you could also add it to the href. Commented Jan 9, 2014 at 23:03
0

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']

0

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.

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.