$_POST not working - php

I am trying to run the below query which basically create a submit button. However when I am trying to click the Accept Request button it is not resolving the if condition and directly echo what is written in else condition.
Can someone help why this would be happening?
<?php
if(isset($_POST['acceptrequest'.$user_from]))
{
echo "you are now freind !";
} else
{
echo 'Error in reading - acceptrequest'.$user_from;
}
?>
<form action = "friend_request.php" method = "POST">
<input type="submit" name = "acceptrequest<?php echo $user_from;?>"
value="Accept Request" style = "margin-left: 5px;" />
<input type="submit" name = "ignorerequest<?php echo $user_from;?>"
value="Ignore Request" style = "margin-left: 5px;" />
</form>

In general, you ought not use variables to define the name of a form input.
Specifically because of the "variable" nature of variables, you can't be sure that the name posted will match the name you're looking for.
In order to keep your application stateless, you should instead post the variable kn its own hidden input.
Below, I modified your form to post user_from as its own input, separate from acceptrequest and ignorerequest.
This should fix any state issues that you were experiencing before.
<?php
if (isset($_POST['acceptrequest')) {
echo "You are now friend with {$_POST['user_from']}!";
} else if (isset($_POST['ignorerequest')) {
echo "You ignored the request from {$_POST['user_from']}!";
} else {
echo 'Error in reading - acceptrequest'.$user_from;
}
?>
<form action = "friend_request.php" method="POST">
<input type="hidden" name="user_from"
value="<?php echo $user_from;?>" />
<input type="submit" name="acceptrequest"
value="Accept Request" style = "margin-left: 5px;" />
<input type="submit" name="ignorerequest"
value="Ignore Request" style = "margin-left: 5px;" />
</form>

Maybe You haven't set all variables. Try to define that and use: $
So create something like: $acceptrequest == something

Related

passing two values (one from text, one hidden) using html and php

I really struggle here and I spent much time on a solution but it seems that I missing something here.
So I have one page that I have 2 values:
$semesterID = $_GET['semesterID'];
$semesterID_old = $semesterID;
I do this as I want to pass 2 values on my next page. I am trying to do that with this:
<form action=ed_semester_v2.php method="post">
<div id="name">
<h2 class="name">Semester ID</h2>
<input class="subjectname" type="text" name="sem_id" id="sem_id" value= "<?php echo $semesterID; ?> " /> <br>
</div>
<input type="hidden" name="semesterID_old" value= "<?php $semesterID_old; ?>"/>
<button type="submit" value="Submit" name="submit">SAVE CHANGES</button>
</form>
The one $semesterID pass fine to the next page but the semesterID_old no. Just to clarify that the semesterid might change by the user and that's ok.
In my next page, I use this code:
if(isset($_POST['submit'])) {
$first_name=$_POST['sem_id'];
$last_name=$_POST['sem_name'];
$semesterID_old= $_POST['semesterID_old'];
if (empty($last_name)) {
echo "Name is empty";
} else {
$query = "UPDATE semesters SET name = '$last_name' where semesterid='$first_name'";
}
if (!mysqli_query($dbconnect, $query)) {
die('An error occurred when submitting your review.');
} else {
// echo "Thanks for your review.";
}
}
echo $first_name;
echo $semesterID_old;
First_name is fine but semesterID_old says that's undefined.
Any help would be much appreciated.
Just change echo semesterID_old; to echo $semesterID_old;

Trying PHP IF Else Statements for the first time

I'm learning PHP and trying to understand the if .. else statements a little better, so I'm creating a little quiz. However, I have come across an issue and I don't seem to know what the issue is. My problem is that whenever I type in the age in the input area, it will give me the $yes variable every time even if I enter the wrong age.
Here is my code so far:
My html file:
<form action="questions.php" method="post">
<p>How old is Kenny?<input></input>
<input type="submit" name="age" value="Submit"/>
</p></form>
My php file:
<?php
$age = 25;
$yes = "Awesome! Congrats!";
$no = "haha try again";
if ($age == 25){
echo "$yes";
}else{
echo "$no";
}
?>
You catch the user input inside the $_POST superglobal var (because the method of your form is POST.
So
<?php
$age = 25;
should be
<?php
$age = $_POST['age'];
There is an error in HTML too. This
<input type="submit" name="age" value="Submit"/>
should be
<input type="text" name="age" value=""/>
<input type="submit" value="Click to submit"/>
Because you want one input and one button. So one html element for each element.
and <input></input> must be cleared because it's not valid syntax :-)
<form action="questions.php" method="post">
<p>How old is Kenny?</p><input type="text" name="age"></input>
<input type="submit" value="Submit"/>
</form>
$age = (int) $_POST["age"];
$yes = "Awesome! Congrats!";
$no = "haha try again";
if ($age == 25) {
echo $yes;
} else {
echo $no;
}
<?php
/* Test that the request is made via POST and that the age has been submitted too */
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['age'] ) ){
/*
ensure the age is an integer rather than a string ..
though for this not overly important
*/
$age=intval( $_POST['age'] );
if( $age==25 ) echo "Congratulations";
else echo "Bad luck!";
}
?>
<form action="questions.php" method="post">
<p>How old is Kenny?
<input type='text' name='age' placeholder='eg: 16' />
<input type="submit" value="Submit" />
</p>
</form>
A simple html form, note that the submit button does not carry the values you want to process, they are supplied via the input text element.
First of all, you need to echo the variable; echoing "$no" will keep it as a string. Remove the quotes from "$no" and "$yes" in your if then statement. Otherwise, your code seems sound!

Php Repeatable region

So i got this code, at the moment it is repeating everything , and i just wanted it to repeat the echo, so i get all usernames from it, if i leave it as it is it will also repeat the form when i press a username. Every time i tried to ajust it, it just gave me syntax errors
<?php do { ?>
<?php
$username = $row_mensagens['username'];
$user = $row_mensagens['id'];
if(isset($_GET['user']) && !empty($_GET['user'])){
?>
<form>
Introduz mensagem : <br>
<textarea name='message' rows='7' cols='60'></textarea>
<br><br>
<input type='submit' value="Send Message" />
</form>
<?php
} else {
echo "<p><a href='mensagens.php?user=$user'>$username</a></p>";
}
?>
<?php } while ($row_mensagens = mysql_fetch_assoc($mensagens)); ?>
that do { } while() will always repeat as many as the number of records come from database.
You can do it this way:
<?php
if(isset($_GET['user']) && !empty($_GET['user'])){
?>
<form>
<input type="hidden" name="user" value="<?php echo $_GET['user']; ?>" /> <!-- hidden field so you can process to who -->
Introduz mensagem : <br>
<textarea name='message' rows='7' cols='60'></textarea>
<br>
<br>
<input type='submit' value="Send Message" />
</form>
<?php
} else {
do {
$username = $row_mensagens['username'];
$user = $row_mensagens['id'];
echo "<p><a href='mensagens.php?user=$user'>$username</a></p>";
} while ($row_mensagens = mysql_fetch_assoc($mensagens));
}
?>
Move do { inside else and show the form only if you have a $_GET['user']
I have also added for you a hidden field, so you know who to send message.
Hope you understand how this works. Documentation on Control Structures: do-while
I also suggest to make that form a post form, as by default it is a get form, and since you have a textarea you are more likely to bump into errors if the message is too long.
LE: Another suggestion, try to move to PDO or mysqli_* functions since mysql_* functions are considered deprecated as of PHP 5.5 and have some good chances to be removed.

Removing the Name button in the address bar

When I try to do
Search in Form
i get in address bar the following entry
check.php?go_btn=&s=banana
Why do I get the name of the button "go_btn"
I should get the value
check.php?s=banana
Why this value
name = "go_btn"
Appears in the address bar
simple_search.html
<form action="check.php" method="get" id="my_form">
<input name="go_btn" type="submit" id="go_btn" form="my_form" formaction="check.php"
formmethod="get"
style="background-image: url(images/go.png); border: solid 0px #000000; width:
71px; height: 55px;" value="">
<input type="text" name="s" id="s">
</form>
check.php
<?php
if (isset($_GET['go_btn'])) {
$s_find = $_GET['s'];
if ($s_find == "banana") {
print ("you find banana");
}
else {
print ("blablabla....");
}
}
?>
You get it because you asked for it:
<input name="go_btn" [..snip...] value="" />
^^^^^^^^^^^
Any <input> field with a name attribute will get submitted along with the rest of the form. If you don't want go_btn being submitted, then take away the name attribute.
Well, In your PHP instead of asking about go_btn ask for s as follows:
<?php
if (isset($_GET['s'])) {
$s_find = $_GET['s'];
if ($s_find == "banana") {
print ("you find banana");
}
else{
print ("blablabla....");
}
}
?>
And then remove the attribute name from your form's element go_btn as follows:
<form action="check.php" method="get" id="my_form">
<input type="submit" id="go_btn" form="my_form" formaction="check.php"
...
You will get it because you asked for it. Your form method is GET and the GET method takes all values with it in the URL. If you would like not to see those values in the URL, you may use the POST method.
Moreover, you shouldn't be having ANY problem with that term in the URL, simple because you need that variable in the other file.

Whois PHP script

I would like to know how can I add a button to enable user to register the available domain name? My idea is to a button linked to registration page next to the available domain name. When the user click the button, the name of the searched domain will be passed to the next page, which is the registration page.
I've added the following code, but it's not working.
function showDomainResult($domain,$server,$findText){
if ($this->tr == 0){
$this->tr = 1;
$class = " class='tr2'";
} else {
$this->tr = 0;
$class = "";
}
if ($this->checkDomain($domain,$server,$findText)){
echo "<tr $class><td>$domain</td><td>$this->daftar_tld($domain)</td></tr>";
}
else echo "<tr $class><td>$domain</td><td class='tak'>TAKEN</td></tr>";
}
// To display register button
function daftar_tld($domain){
?>
<form method=post action="http://www.com">
<center><input value=$_POST["$domain"]><input type="submit" name="butangDaftar" class="sbtn" value="Register" /></center>
</form>
<?php
}
?>
And the output that I get is this (image is provided at the link below):
http://lh5.ggpht.com/_d7N0eqyUoUw/TGgPvCt4B3I/AAAAAAAAAHY/T7e4KjVNT04/Screen%20shot%202010-08-14%20at%2012.53.59%20PM.png
I really hope that someone can help me. I've been working on this for days. Phewwww...
You haven't set a name or type for the input variable holding the domain:
Your code:
<center><input value=$_POST["$domain"]><input type="submit" name="butangDaftar" class="sbtn" value="Register" /></center>
Suggested code:
<center><input type="hidden" name="domainToRegister" value="<?php echo $_POST["$domain"]; ?>" /><input type="submit" name="butangDaftar" class="sbtn" value="Register" /></center>
The form will then submit the domain as $_POST['domainToRegister'].

Categories