开发者

just php no javascript

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 somethi开发者_Go百科ng different is suppose to happen, is this possible, if so, what do I need to read?


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.


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.


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>


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜