echo "<input value='$variable' type='submit' id=submit' name='submit'>";

That variable is an array which keeps repeating and changing numbers. (e.g. 1-20).

Now using a switch statement to access the input submit button when pressed:

switch($_POST[submit]) {
case '':
break;
}

I cant figure out what code to construct and use to check if one of the numbers has been selected (apart from the obvious 1-20 which is not an option).

Any Ideas?

Thanks, Regards X

I cant figure out what code to construct and use to check if one of the numbers has been selected (apart from the obvious 1-20 which is not an option).

What exactly do you mean by that ? Do you want to access the value of $variable when submit button is clicked ? If thats the case, then the simplest method would be to assign the value of $variable to a hidden form variable(using the onclick event).

Hmmm ok.

For example say the variable is:

//Note: I have alot of input buttons with SAME NAME/ID but DIFFERENT VALUES (but there all integer)

$submitNumber= 100 // Then changes to 78, Then changes to 64, etc

<input type="submit" value="$submitNumber" name="submit" id="submit">

switch($_POST[submit]) {
case "$submitNumber":
break;
}

Then whenever any of the values from $submitNumber is clicked it selects that switch statement's case. I was thinking maybe making all the $submitNumber integers and then searching the switch statement and have a case that if any number are selected do this?

I know its confusing but I hope that helps =(

Thanks for your help, Regards X

You can access the value of the submit button using $_POST. You can then use it in a switch statement or a if loop. :confused:

How can you have input buttons with same name ? For example, If there are 2 buttons with the same name, how can you validate if that particular button was clicked ?

You can do that because they both call the switch statement and then depending on the value of the input statement, X occurs.

Im trying to find some sort of case to put in the switch statement that detects "any integer value". Any ideas?

Calling switch statement is fine. But Say for example you have 2 textboxes with the same name. While requesting the values, dont you think the value of 1 textbox will be overwritten by the other ? But anyway, coming back to your question. If you want a function to detect any integer value, you can make use of is_int.

ya wouldnt work with textfields just buttons unless u were referencing the value of the text field. Anyways ya i tried is_int wasnt working.

I was using is_int like this:

is_int($variable)? "YES":"NO";

This wasnot working as a case then i tried to make it a seperate variable and the use the case to call YES but that didnt work either =(

Maybe an example of is_int you could demonstrate then I can try fiddle around and implement it in my webpage? Thanks

ive worked something out uisng is_int() but as i had the case as default and the a header the page is coming up as white. ZZZZzzzz maybe once i get back from work I can try work something out. =(

I don't think you can use is_int() since all HTTP data is considered strings. There is no type distinction in HTML FORMS or HTTP GET/POST responses.

You could check for range:

if ($var > 0 && $var < 100) {

}

Or you can use something like:

if (intval($var) > 0) {}

this would make strings equal to 0. So would work unless you also wanted to test specifically for 0.

if (intval($var) > 0 || $var === '0') {}

if anything, you could revert to using regex. But for simple tests I wouldn't invoke Regex as its a pretty heavy application for such a simple task.

I'm sure there is a simple solution to your problem.

commented: Thanks for the helpful advice =) +1

I came up with a solution using an if statement and session. Thanks for all the help though people =)

Instead if the "if statement and session" which is not elegant php coding you might try this:

<input type="submit" name="do" value="Insert Record"  />
<input type="submit" name="do" value="Delete Record" />


<?php
switch ($_POST)
{
case 'Insert Record': echo "insert record";
case  'Delete Record': echo "record deleted";
}
?>

forgot the break command

i.e.


<?php
switch ($_POST)
{
case 'Insert Record': echo "insert record";break;
case 'Delete Record': echo "record deleted";break;
}
?>

Ya thanks for the input but thats what i was using already i need something else hence I used an if statement with a session statement also. =)

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.