Hi

I have programmed a 5x5 'grid' of square buttons and need to store the buttons the user clicks on (in order) so that i can ask them to recall the sequence later. I think this requires server side scripting and installed XAMPP for PHP on my mac...

I have looked through some forums and think i need to do something with the onclick function when a button is clicked. Keep getting error messages when running :)

Suggestions welcome

Thanks

You are right about the onclick function. The onclick function is a JavaScript event handler so you'll need to incorporate JavaScript (else you'll have to have a page refresh after each button is clicked). Here is a link to a memory game tutorial written in JavaScript and PHP. I believe this is similar to what you are asking.

commented: nice example +1

cheers dude...ill have a look

You are right about the onclick function. The onclick function is a JavaScript event handler so you'll need to incorporate JavaScript (else you'll have to have a page refresh after each button is clicked). Here is a link to a memory game tutorial written in JavaScript and PHP. I believe this is similar to what you are asking.

I've checked the page and the game doesnt even work lol...didnt find anything on the click function or php?? i think i might need to create a seperate php form to handle the javascript button actions??

Hi

I have programmed a 5x5 'grid' of square buttons and need to store the buttons the user clicks on (in order) so that i can ask them to recall the sequence later. I think this requires server side scripting and installed XAMPP for PHP on my mac...

I have looked through some forums and think i need to do something with the onclick function when a button is clicked. Keep getting error messages when running :)

Suggestions welcome

Thanks

If you will be refreshing the page, then you'll need some form of persistent storage. The choices are to store the results in a client side cookie, or server site, such as with PHP.

If you don't have to refresh the page, then you'll just need to store in memory, which means pure JavaScript and storing to a variable.

A simple example:

Some HTML:

<button id="a" onclick="clicked('a')">Click A</button>
<button id="b" onclick="clicked('b')">Click B</button>

The HTML is just two buttons. Each one has an onclick event handler. The handler invokes the function "clicked" and passes it an alphabet matching the id of the button clicked.

Now some JS:

// Array to save clicks to
clicks= [];

// this function saves the click to an Array
function clicked(alphabet) {
  clicks.push(alphabet);
}

Teh function clicked() saves the alphabet passed to it to the array, clicks.
push() is a method of the Array object, that adds an item onto its list.

If you wanted to display that, you could iterate through each item in the list, and display it. If you wanted to save it, you'd have to send it to a cookie, or to the server...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.