0

I'm just learning php and one thing I searched whole web with no luck. I know following thing is pretty simple in client side script (javascript), but I want to know whether is it possible in php.

I have a html button element, using php code can I change its text? Something to change over and over each time button is clicked.

3 Answers 3

2

Just define the button value via a variable:

<?php 
$buttonName = "bla bla";
?>

<input type="submit" value="<?php echo $buttonName; ?>" />

OR

<button><?php echo $buttonName ?></button>

Then you can do with the variable whatever you want

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

Comments

1

PHP can only manipulate the source code of a page at the point that the page is initially rendered, because it executes on the server before the page is sent to the client. You could have the button label changing if the button caused the page to reload on every click.

For example, to give a button a random number on every load you could do something like:

<button><?php echo rand(1,10); ?></button>

But this would be dependant on the button causing the page to reload. If you want to do this without page reloads then JavaScript is the right tool for the job.

4 Comments

In addition to this, I suggest you use javascript with jquery for interaction stuff. PHP is for structure
Absolutely, JS is the tool for the job.
Thank you very much for reply. I knew It is a simple work in javascript but I wanted to know whether it is possible in php.
Only as I have described, because PHP is only executed on the server.
0

You can send the current text of the button to the server when clicking the button and compare it with the new random text, if they are not equal, echo it to the page, if they are, continue searching for a non-equal rand text.

Also you can do it with sessions. Put current text in a session var, and create another one in a submission, and compare it to the previously created session var, and echo to the page.

<?php
session_start();
if (isset($_POST)) // check button submission - do it with any way you like
{
    while(1)
    {
        $new_rand = rand();
        if($new_rand != $_SESSION['currText'])
        {
           $curr_text = $new_rand;
           $_SESSION['currText'] = $curr_text;
           echo "<button>$curr_text</button>";
           break;
        }
    }

}
else // and here is the first page request, when no button is clicked
{
    $new_rand = rand();
    $_SESSION['curr_text'] = $new_rand;
    echo "<button>$new_rand</button>";
}

?>

With this you're going to get completely different texts at each time of pressing the button ;)

In this code and the scenarios above, I talked about random numbers, because PHP rand() function returnes numbers. If you want texts, you can put all the texts you want to show in your page, in an array, and get random one using rand(0, $max_index_of_the_array) - show the text in the array where the text's index came from the rand(0, $max_index_of_the_array).

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.