now this is my html button
<html>
<head>
</head>
<body>
<div>
<button type="button">change header</button>
</div
</body></html>
what i want is when i click on it change the variable
example
<?php
$var= "path1";
$new= "path2";
if ( some one clicked the button) !== false) {
$var = $new;
header('Location: $var');
} else {
header('Location: $var');
}
?>
if there is a method with javascript please post it please
now this is my html button
<html>
<head>
</head>
<body>
<div>
<button type="button">change header</button>
</div
</body></html>
what i want is when i click on it change the variable
example
<?php
$var= "path1";
$new= "path2";
if ( some one clicked the button) !== false) {
$var = $new;
header('Location: $var');
} else {
header('Location: $var');
}
?>
if there is a method with javascript please post it please
Share Improve this question edited Aug 19, 2017 at 13:29 SuperDJ 7,66111 gold badges43 silver badges78 bronze badges asked Aug 19, 2017 at 11:29 reko bekoreko beko 861 gold badge1 silver badge10 bronze badges 3- 4 Possible duplicate of Simple PHP: getting variable from a form input – user8461611 Commented Aug 19, 2017 at 11:31
- didnt get it clear... do you have form tag in html. if u wont use form tage its not possible by php. – Farsay Commented Aug 19, 2017 at 11:35
- @mdus2r no not the same purpose – reko beko Commented Aug 19, 2017 at 11:46
6 Answers
Reset to default 2With PHP you can try the following:
<form action="" method="post">
<input type="hidden" value="t">
<button>change header</button>
</form>
Add the post check to see if the page is posted.
<?php
$var= "hello world";
$new= "i hate world";
if ( $_POST ) {
$var= $new;
echo $var;
} else {
echo $var;
}
But my guess is that you're really looking for a JS way of doing it.
<html>
<head>
</head>
<body>
<div>
<button type="button" onClick="function()">change header</button>
</div>
</body></html>
function(){
$var= "hello world";
$new= "i hate world";
$var= $new;
echo $var;
echo $var;
}
Php is a server side language. You can use javascript like this or if you really want to use php you need to submit the form
PHP is server side so it is executed before the page/html is sent to the user. any user interactions are handled by Javascript in the browser. if you wish to change content when a user clicks a button the use javascript event handlers
<html>
<head>
</head>
<body>
<div>
<button type="button" onclick="changeStuff()">change header</button>
</div>
<script>
function changeStuff(){
//change header code
}
</script>
</body></html>
reko beko@@ see this is hundred percent working
<form action="" method="post">
<input type="hidden" value="t">
<input type="submit" value="change header" name="submit">
</form>
<?php
$var= "hello world";
$new= "i hate world";
if (isset($_POST['submit']) ) {
$var= $new;
echo $var;
} else {
echo $var;
}
?>
<html>
<head>
<script>
function myfunction(){
// no variable needed for this logic
//window.location.replace('the redirect path when button is clicked ')
// example
window.location.replace('newsletter_tb.php');
}
</script>
</head>
<body>
<div>
<button type="button" onclick="myfunction()">change header</button>
</div
</body>
</html>
Using javascript is best for this question
PHP is server side scripting language
<button type="button" id="chnageHeaderbtn">change header</button>
Put in script
$("#chnageHeaderbtn").click(function() {
var prevUrlAddress='http://www.myfirsturl.';
var chnageUrlAddress='http://www.myurl.';
window.location.href =chnageUrlAddress;
});