Reputation: 477
In following function if the input is blue then it is giving result as: "header content goes here Your favorite color is blue!" whereas it is supposed to give- "Your favorite color is blue!", please help me correcting it. If input is red then it is ending statement here as: "header content goes here" which is correct.
Please forgive me if I am asking silly question as I am new to PHP and I don't know if it is ok or not to use function as variable in switch case.
test1.php
<?php
function header2() { ?>
header content goes here
<?php }
$good =header2();
$Input2 = $_POST['input'];
switch ($Input2) {
case "red":
echo $good;
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
main.js
$('[id=stringone]').click(function(){
var string =$('[id=stringotherone]').val();
$.post('test1.php',{input: string}, function(data){
$('[id=stringotherthanone]').text(data);
});
});
test.php
<! doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="css/hwcb.css">
</head>
<body>
<input type="button" value="Go" id="stringone">
<input type="text" id="stringotherone"/>
<p><div id="stringotherthanone"></div></p>
<script src="jquery.js"></script>
<script src="js/css.js"></script>
<script src="main.js"></script>
</body>
</html>
Upvotes: 0
Views: 143
Reputation: 23882
You need to return
in the function otherwise it outputs when the function is called.
Example: https://3v4l.org/1Ptml vs. https://3v4l.org/ug778
$good =header2();
is outputting the header content goes here
, not the echo $good;
in the switch
. That actually does nothing because that variable is empty.
Upvotes: 3
Reputation: 9465
Change
<?php
function header2() { ?>
header content goes here
<?php }
to
<?php
function header2() {
return 'header content goes here';
}
Upvotes: 1