0

Just using php, is it possible to create a button via html that reacts to the user's input, for example, if the user clicks the button 4 times, something is suppose to happen, or do I need javascript.

Likewise if the user clicks the button twice or three times something different is suppose to happen, is this possible, if so, what do I need to read?

3
  • 2
    make distinction between Java and javascript - they are totally different things Commented Dec 6, 2009 at 8:17
  • Java:Java is a programming language that derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture. Commented Dec 7, 2009 at 7:12
  • Javascript:JavaScript is an object-orientedscripting language used to enable programmatic access to objects within both the client application and other applications. It is primarily used in the form of client-side JavaScript, implemented as an integrated component of the web browser, allowing the development of enhanced user interfaces and dynamic websites. Commented Dec 7, 2009 at 7:13

5 Answers 5

2

Yes it is possible with just PHP. You could carry the state of what has been inserted along with sessions or put it back into the form so that it’s submitted with the next insertion.

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

3 Comments

I guess that's the only way to do this, thanks, I will read up on sessions.
Sessions aren't very good option here. Imagine the same user having more tabs opened. If we are talking about submit button, it might be the only option tho. If it's simple button adding some content via Javascript, it may get us into a trouble. I personally prefer to use as few sessions as I can (mostly just for login and ACL), as they often start to smell of nasty global variables. In your case, you'll also have to keep unsetting that session at some point.
@ondrowan: Session != cookie. Cookies are used to identify and track sessions, but the cookie is not the session. The cookie gets passed back and forth between the server and browser, but the session normally exists solely on the server. (While you technically can stuff the entire session state into a cookie, it's almost always a very bad idea to do so.) And this can be done without sessions or cookies by stuffing the number of clicks into the URI string or a hidden field on the form, either of which would make the count specific to a single tab.
1

Do you mean as in real time? In that case, no, it is not possible.

You could use sessions to track submits, but without the use of of JavaScript (Ajax) the user would have to watch the page reload for 4 clicks. If your going to use Ajax you might as well just code some JavaScript to send data based on click sequences.

In reality you need JavaScript.

Comments

1

If the button is going to do an action without refreshing the webpage, then PHP can never do that for you.

Likewise, if you don't mind the page refreshing each time the button does an action. You can wrap the button in a form that posts GET/POST(to be secure) values for the PHP script to read.

<?
 $times = $_GET['timesClicked'];
 $times++;
?>
<form method="get" action="your script">
<input type="hidden" name="timesClicked" value="<?= $times; ?>">
<input type="submit" value="your button">
</form>

Comments

0

This is ideal use-case for using Javascript. You will need to bind your custom function to elements onclick event. Here is a sample code you can include into your html code. It assumes you've specified button id:

<script>

var clicks = 0;

function yourfunction() {
click++;
if (clicks == 4) alert ('Your clicked 4 times!')
}

document.getElementById('elementId').onchange = yourfunction;
</script>

Comments

0

If it's acceptable to you for the browser to load the page anew with each click, then, yes, this is quite possible with PHP alone, using either a cookie, a server-side session, the URI query string (i.e., ?num_clicks=2 at the end of the URL), or a hidden form field to track the number of clicks. If you really wanted to, you could even do it in plain HTML by creating a separate page for each stage/state and looping through them, advancing one step on each click.

If you want the page to react to the click immediately without contacting the server or if you want to refresh only a portion of the page without reloading the whole thing, then, no, that would require JavaScript.

Comments

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.