php- get postback from a hyperlink - php

I have a HTML form in which I'm having the following hyperlink:
What's New</li>
I want to get a postback from this hyperlink, in order to execute the following PHP code:
if($_POST['WhatsNew'] == "Y")
{
echo "HELLO USER";
}
How can I do so?

your html where you can send id by using ? like this
What's New
and if you are sending some value through url you have to use $_GET instead of $_POST like below
$whatsnew=$_GET['whatsnew'];
if(!empty($whatsnew) && $whatsnew == 1)
{
echo "HELLO USER";
}

You can do it by using Query Strings
What's New
Your PHP
if(isset($_GET['whatsnew']) && $_GET['whatsnew'] == 1)
{
echo "HELLO USER";
}

Related

PHP - Add and stack a value with a session

Let's say I have a $variable, which contains the word "hey" and a link with a GET parameter.
HTML
Link
PHP
$var = "hey";
if ($_GET['add'] == 'true') {
$var .= "2";
echo $var;
}
When I click on the link, it will add "2" to the variable value "hey", so the
output is: hey2.
Is there a way of keep adding (stacking) the numbers on click with pure PHP?
If i click again, I'd like to have hey22, hey222 (...)
EDIT: It looks like it isn't going to work with variables, so the best answer goes to #Bunker Boy because he solved it with sessions.
#Syno try this:
// mywebsite.html
Link
//mywebsite.php
<?php
session_start();
if(isset($_SESSION["var"])){
if ($_GET['add'] == 'true') {
$_SESSION["var"] .= "2";
echo $_SESSION["var"];
}
}
else{
$_SESSION["var"] = "hey";
if ($_GET['add'] == 'true') {
$_SESSION["var"] .= "2";
echo $_SESSION["var"];
}
}

PHP If / Else statement going directly to the Else without waiting for form input

Ok so I have a form with 1 input and a submit button. Now I am using an if/else statement to make three acceptable answers for that input. Yes, No, or anything else. This if/else is working the thing is the code is kicking out the else function as soon as the page is loaded. I would like there to be nothing there until the user inputs then it would show one of three answers.
Welcome to your Adventure! You awake to the sound of rats scurrying around your dank, dark cell. It takes a minute for your eyes to adjust to your surroundings. In the corner of the room you see what looks like a rusty key.
<br/>
Do you want to pick up the key?<br/>
<?php
//These are the project's variables.
$text2 = 'You take the key and the crumby loaf of bread.<br/>';
$text3 = 'You decide to waste away in misery!<br/>';
$text4 = 'I didnt understand your answer. Please try again.<br/>';
$a = 'yes';
$b = 'no';
// If / Else operators.
if(isset($_POST['senddata'])) {
$usertypes = $_POST['name'];
}
if ($usertypes == $a){
echo ($text2);
}
elseif ($usertypes == $b){
echo ($text3);
}
else {
echo ($text4);
}
?>
<form action="phpgametest.php" method="post">
<input type="text" name="name" /><br>
<input type="submit" name="senddata" /><br>
</form>
You just need to call the code only when the POST value is set. This way it will only execute the code when the form was submitted (aka $_POST['senddata'] is set):
if(isset($_POST['senddata'])) {
$usertypes = $_POST['name'];
if ($usertypes == $a){
echo ($text2);
}
elseif ($usertypes == $b){
echo ($text3);
}
else {
echo ($text4);
}
}
Just put the validation in the first if statement like this:
if(isset($_POST['senddata'])) {
$usertypes = $_POST['name'];
if ($usertypes == $a) {
echo ($text2);
} elseif ($usertypes == $b) {
echo ($text3);
} else {
echo ($text4);
}
}
When you load your page the browser is making a GET request, when you submit your form the browser is making a POST request. You can check what request is made using:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Your form was submitted
}
Put this around your form processing code in order to keep it from being executed on GET request.

Convert classis asp if statement to php

Am still very new to PHP and need to convert existing pages to php, could anyone show me how to convert the code below to php please?
<%If rsMembership.Fields.Item("MemberOfGroup").Value = "0" then response.write ("Main Member")Else response.write ("Belongs to Member = ")&(rsMembership.Fields.Item("MemberOfGroup").Value)End if%>
Update: This works, not sure if its correct, cant seem to be able to put value as ="0", only works if I put <"1"
<?php
if ($row_rsMembership['MemberOfGroup']< "1")
{
echo "Main Member";
}
else
{
echo "Belongs to Member =" . $row_rsMembership['MemberOfGroup'];
}
?>
To compare a value in a PHP If statement use == and not =, i.e.:
if ($row_rsMembership['MemberOfGroup'] == "0")
{
echo "Main Member";
}
else
{
echo "Belongs to Member =" . $row_rsMembership['MemberOfGroup'];
}

PHP and JavaScript array attachmant

I have the following problem:
onclick=langedit('".$carray2['example']."','".$carray2['id']."')
$carray2['example'] is a string containing "ABOUT US" with space, and this is the output in HTML:
onclick='langedit('Contact" us','9')="">
My PHP code :
else {
$cquery2 = mysql_query ("SELECT id,example,".$startlan." FROM language WHERE example = '$word'");
$carray2 = mysql_fetch_array($cquery2);
if($carray2[$startlan] == '') {
if($_SESSION['view'] == 'admin' && isset($_SESSION['siteshow'])){
echo "<span class='langedit' id='langedit".$carray2['id']."' onclick=langedit('".$carray2['example']."','".$carray2['id']."')><img src='images/sys/edit.png' > </span>";
return $carray2['example'];
}else{
return $carray2['example'];
}
}
I don't know what's happening, the langedit function does not work because of this.
Cover the langedit call on onclick with double quotes
onclick=\"langedit('".$carray2['example']."','".$carray2['id']."')\"
Otherwise if $carray2['example'] is About Us, the onclick attribute becomes onclick=langedit('About which is an invalid function call.

PHP function that decides which HTML code to use?

I'm looking for a function like and if else statement for php which will execute certain html code.
For example:
<?php>
$result = 1;
if ($result == 1)
<?>
html code
else
html code
So, based off the result variable gotten from php scripts, a certain html page is output. I've tried echoing the entire html page, but it just displays the html code-> tags and such.
Hopefully you get what I'm trying to get across, ask if you need any clarification questions. Thanks!
That should work:
<?php
$result = 1;
if($result==1) {
?>
html code
<?php
} else {
?>
html code
<?php
}
?>
The problem I'm facing with the if else statement, is in order to display the html, I have to exit php coding. Thus, the if else statement will not work. (Link)
This is not entirely true. You can use the approach below:
<?php
// Do evaluations
if ( $result == "something" )
{
?>
<p>Example HTML code</p>
<?php
} elseif ( $result == "another thing")
{
?>
<span>Different HTML code</p>
<?php
} else {
?>
<h4>Foobar.</h4>
<?php
}
// Rest of the PHP code
?>
Or, if you don't want to exit PHP coding, you can use the echo or print statements. Example:
<?php
// Evaluations
if ( $result == "foo" )
{
echo "<p>Bar.</p>";
} else {
echo "<h4>Baz</p>";
}
// Some else PHP code
?>
Just be careful with proper sequences of ' and " characters. If your HTML tags are to have arguments, you should watch your step and use either of the following approaches:
echo "<span class=\"foo\">bar</span>";
echo '<span class="foo">bar</span>";
If you want to evaluate some PHP and print the HTML results later, you could use something like this
<?php
$output = "";
if ( $result == "something" ) {
$output = '<p>Example HTML code</p>';
} else if ( $result == "another thing") {
$output = '<span>Different HTML code</p>';
} else {
$output = '<h4>Foobar.</h4>';
}
// Output wherever you like
echo $output;
?>
EDIT (because I'm not sure what you;re trying to do so i'm just putting out different ideas):
If you're trying to output an entire page, it may be useful to use header('location: newPage.html'); instead of $output. This redirects the browser to an entirely new web page. Or you can likely include newPage.html as well.
very close:
<?php
$result = 1;
if ($result == 1){
?>
html code
<?php } //close if
else {
?>
html code
<?php
} //close else
?>
you can echo html code something like this
<?php
$result = 1;
if ($result == 1){
echo "<h1>I love using PHP!</h1>";
}
?>
this would output if Result is 1
**I love using PHP!** //but slightly bigger since its H1

Categories