PHP $_POST returns null values - php

This is my html code:
<form style="width: 20%; margin: auto;" action="subscribe.php" method="post" id="subscribeToNews">
<fieldset>
<legend>Subscribe:</legend>
<label for="subName">First Name:</label><br /><input type="text" id="subName" name="subName" /><br />
<label for="subEmail">Email:</label><br /><input type="text" id="subEmail" name="subEmail" /><br />
<input style="width: inherit;" type="submit" value="Subscribe" />
</fieldset>
</form>
This is the subscribe.php file:
<?php
$con = mysqli_connect('95.76.197.98','root','','accounts');
print_r($_POST);
if (isset($_POST["subName"]) && isset($_POST["subEmail"])){
$subUser = $_POST["subName"];
$subEmail = $_POST["subEmail"];
echo "$subUser"."<br />"."$subEmail";
}
?>
I have really tried a lot of things out there on the Internet and nothing seems to work for me. Any ideas?
Also looks like the get method works for this...

Could by related to your nginx configuration.
Try:
$post = file_get_contents("php://input");

This should work:
<form style="width: 20%; margin: auto;" action="subscribe.php" method="post" id="subscribeToNews">
<fieldset>
<legend>Subscribe:</legend>
<label for="subName">First Name:</label><br /><input type="text" id="subName" name="subName" /><br />
<label for="subEmail">Email:</label><br /><input type="text" id="subEmail" name="subEmail" /><br />
<input style="width: inherit;" type="submit" value="Subscribe" />
</fieldset>
</form>
<?php
$con = mysqli_connect('95.76.197.98','root','','accounts');
if($_POST) {
$subName = mysqli_real_escape_string($con, strip_tags($_POST['subName']));
$subEmail = mysqli_real_escape_string($con, strip_tags($_POST['subEmail']));
if(isset($subName) && !empty($subName) && isset($subEmail) && !empty($subMail)) {
echo 'Name: '.$subName.'<br> Email: '.$subEmail;
}
}
?>

Related

How to add a CSS class on HTML button via PHP

I have registration form and I need to hide submit button. This button will be visible after action in PHP.
I tried to use CSS classes and it works, but I don't know how to add a class with PHP.
Here is button with class I want to change:
<form method="post" action="register.php" name="registerform">
<input type="submit" name="register" value="registrovat" id="button8" class=I don't know what /><br><br>
</form>
My CSS:
#button8{
visibility: hidden;
font-family: century gothic;
color: #0099FF;
border: 2px solid #0099FF;
background: transparent;
}
#button8.visible{
visibility: visible;}
I need to change to button8.visible but in PHP (I have condition depend on MySQL data).
EDIT:
I need it because, the button will be visible based on code. User put the code, press a button. This action will run PHP: connect to mySQL find if this code is in database. If yes I need my button to be visible. Because of my low knowledge, I want to use if condition in the same PHP session where is mySQL. Here is the whole code:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<title>Panákovač 4.0</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
</head>
<body>
</body>
</html>
<?php
// show potential errors / feedback (from registration object)
if (isset($registration)) {
if ($registration->errors) {
foreach ($registration->errors as $error) {
echo $error;
}
}
if ($registration->messages) {
foreach ($registration->messages as $message) {
echo $message;
}
}
}
?>
<!-- register form -->
<form method="post" action="register.php" name="registerform">
<input id="login_input_username" class="login_input" type="text" pattern="[a-zA-Z0-9]{2,64}" name="user_name" placeholder="uživatelské jméno" required /><br>
<input id="login_input_password_new" class="login_input" type="password" name="user_password_new" pattern=".{6,}" placeholder="heslo (min. 6 znaků)" required autocomplete="off" /><br>
<input id="login_input_password_repeat" class="login_input" type="password" name="user_password_repeat" pattern=".{6,}" placeholder="zopakujte heslo" required autocomplete="off" /><br>
<input id="login_input_firstname" class="login_input" type="text" name="user_firstname" placeholder="jméno" required /><br>
<input id="login_input_lastname" class="login_input" type="text" name="user_lastname" placeholder="příjmení" required /><br>
<input id="login_input_email" class="login_input" type="email" name="user_email" placeholder="e-mail" required /><br>
<input id="login_input_company" class="login_input" type="text" name="user_company" placeholder="firma" required /><br>
<input type="submit" name="register" value="registrovat" id="button8" class= /><br><br>
</form>
<form method="post" action="register.php" name="registerform">
<input id="login_input_code" class="login_input" type="text" name="user_code" placeholder="ověřovací kód" required /><br>
<input type="submit" name="kod" value="ověřit kód"/><br>
</form>
<?php
if(isset($_POST['kod'])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "login";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user_code = $_POST['user_code'];
$sql = "SELECT `used`,`code` FROM `regcode` WHERE `code` LIKE '$user_code' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "kód: " . $row["code"]. " - použito: " . $row["used"]. $zob . "<br>";
$pouzito = $row["used"];
MY CONDION FOR CLASSES
}
} else {
echo "chybný kód" . $user_code;
}
$conn->close();
}
?>
You can add a php if condition inside the html content like this:
<form method="post" action="register.php" name="registerform">
<input type="submit" name="register" value="registrovat" id="button8"
class="<?php if(condition) { echo 'classname'; } ?>" />
</form>
In this case if the condition is true it will echo the class inside the class attribute, there are other ways to do it but this is the most intuitive way for new PHP developers.
Like #Jordi Nebot told you can just do the short version like:
<input class="<?= ($condition) ? 'classname' : ''"> ?>
only way in PHP is to echo the output
echo '<input type="submit" name="register" value="registrovat" id="button8"
class="class1">';
echo '<input type="submit" name="register" value="registrovat" id="button8"
class="class2">';
etc...
or
$class = "class1";
?>
input type="submit" name="register" value="registrovat" id="button8"
class="<?php echo $class; ?>">
<?php
$class = "class2";
?>
input type="submit" name="register" value="registrovat" id="button8"
class="<?php echo $class; ?>">
<?php
<?php $classname = condition ? 'class8' : 'defaultClass'; ?>
<form method="post" action="register.php" name="registerform">
<input type="submit" name="register" value="registrovat" id="button8" class="<?php echo $classname; ?>" /><br><br>
</form>
You can use ternary operator like:
<form method="post" action="register.php" name="registerform">
<input type="submit" name="register" value="registrovat" id="button8" class"<?= $condition ? 'classvisible' : 'classhidden'"?> />
</form>
I got it. I didn't know, that PHP has to be placed above HTML if i want to use variable from PHP.
IN PHP:
...//some condition
If ($row["used"] == 0){
$myclass ="zobrazeno";
}
...
In HTML
<input type="submit" name="register" value="registrovat" id="button8" class=<?php echo $myclass; ?> /><br><br>
In CSS:
#button8{
visibility: hidden;
font-family: century gothic;
color: #0099FF;
border: 2px solid #0099FF;
background: transparent;
#button8.zobrazeno{
visibility: visible;

PHP passing a variable across pages

I am having issues with a piece of code in Joomla. It may be something to do with the plugin that enables the PHP but in case it isn't.
Page 1 has a form
<form action="/index.php/bridge" method="POST" name="postcode">
<div><input style="height: 50px;" type="text" placeholder="Enter Your Postcode..." /> <input type="submit" value="Get Started today!" /></div></form>
The text you input becomes the variable I want to pass over
On Page 2
<?php echo "test";
$postcode=1;
$poster=$_POST['postcode'];
echo $poster;
// You can place PHP like this
?>
Unfortunately, the postcode isn't echoed
Assuming nothing else is the cause of this error, try naming the input you are sending over to postcode:
<form action="/index.php/bridge" method="POST">
<div>
<input style="height: 50px;" name="postcode" type="text" "placeholder="Enter Your Postcode..." />
<input type="submit" value="Get Started today!" />
</div>
</form>
In your PHP code you are echoing $_POST['postcode'] but you are not sending the same variable at the time of form submission from input attribute.
<form action="/index.php/bridge" method="POST" name="demoForm">
<div>
<input style="height: 50px;" name="postcode" type="text" "placeholder="Enter Your Postcode..." />
<input type="submit" value="Get Started today!" />
</div>
</form>
Try this
<form action="/index.php/bridge" method="POST">
<div>
<input name="postcode" style="height: 50px;" type="text" placeholder="Enter Your Postcode..." />
<input type="submit" name="submit" value="Get Started today!" />
</div>
<?php
if (isset($_POST['submit'])){
echo $poster = $_POST['postcode'];
}else{
echo $poster = 1;
}
?>

Comment the same again and again

I am trying to make a comments section, where people can comment stuff on my webpage.
All the comments get to a database. Alle that works fine.
The only problem I have is, when i have commented some stuff and reload the webpage it comment the same thing again.
Is there a if statement or something to prevent this?
while ($info = mysql_fetch_array($result)){
echo '<div style="border-style: solid; border-color: #808080; border-width: thin">
<div style="width: 1%"><p style="font-size: 10px; margin: 0px">'.$info['Navn'].'</p></div>
<p>'.$info['Besked'].'</p>
</div>';
}
if (isset($_POST['navn']) && isset($_POST['besked']) && isset($_POST['submit'])) {
$navn2 = $_POST['navn'];
$besked2 = $_POST['besked'];
$data = "INSERT INTO `tester`.`davs` (`Navn`, `Besked`) VALUES ('$navn2', '$besked2');";
$resultalt = mysql_query($data);
if ($resultalt) {
echo "$resultat";
}else{
echo "$resultat";
}
mysql_close();
}
?>
<form action="database.php" method="post" id="commentform">
<label for="comment_author" class="required">Your name</label>
<input type="text" name="navn" id="comment_author" value="" tabindex="1" required="required">
<label for="comment" class="required">Your message</label>
<textarea name="besked" id="comment" rows="10" tabindex="4" required="required"></textarea>
<input type="hidden" name="comment_post_ID" value="1" id="comment_post_ID" />
<input name="submit" type="submit" value="Submit comment" />
</form>
my php code:
http://pastebin.com/bQ7c1MPD
my inputs:
http://pastebin.com/P9uc6Hhz
Use tokens in your HTML form:
<input type="hidden" name="token" value="<?=$_SESSION['token'] = uniqid(); ?>" />
This require this at the top of the PHP script (before any output):
session_start();
For validation:
if(isset($_POST['submit']) && $_POST['token'] == $_SESSION['token']))
Full code: PHP | Form

issue with HTML and PHP language

I have issue with my login page, when I add my code don't display any action.
please see the below
Index.html
<form >
<fieldset style="width:355;">
<legend></legend>
<div id="Form">
<br><p><b> Log in</b></p>
<br>
</div>
<div class="col1 pad_left1">
<form name="logForm" onsubmit="return log()" action="log_exe.php" method="POST" >
<label style="font-size:100%;" >Email:</label> </br>
<input type="text" name="email1" placeholder="user#hotmail.com" id="email1" style="border:1px solid black; width:80%;" /> <b style="color:red;">*</b> </br><br>
<label style="font-size:100%;" > password:</label> </br>
<input type="password" name="pass1" placeholder="Password" id="pass1" style="border:1px solid black; width:80%;" /> <b style="color:red;">*</b>
<b style="display:none; color:red;" id="hide1">Sorry !! Wrong username or password</b>
</br>
<br>
<input style="font-size:120%;width:40%;padding:1%" type="submit" value="Log in" name="login" /></br><br>
</form>
</div>
</fieldset>
</form>
log_exe.php
<?php
session_start();
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASSWORD = '';
$DB_DATABASE = 'la';
$con=mysql_connect($DB_HOST,$DB_USER,$DB_PASSWORD);
if(!$con)
{
die('Failed to connect to server: '.mysql_error());
}
$db=mysql_select_db($DB_DATABASE);
if(!$db)
{
die("Unable to select database");
}
$email =$_POST['email1'];
$pass =$_POST['pass1'];
$qry="SELECT Email FROM user WHERE Email='".$email."' AND Password='".$pass."';";
$qry2="SELECT email FROM Admin WHERE email='".$email."' AND password='".$pass."';";
$result=mysql_query($qry);
$result2=mysql_query($qry2);
if(mysql_num_rows($result) == 1)
{
$admin = mysql_fetch_assoc($result);
$_SESSION['Email'] = $admin['Email'];
header("location: schedule.php?Email=".$admin['Email']);
exit();
}
if(mysql_num_rows($result2) == 1)
{
$admin = mysql_fetch_assoc($result2);
$_SESSION['Email'] = $admin['Email'];
header("location: controlpanel.php?Email=".$admin['Email']);
exit();
}
else
{
die ("<script> window.confirm(\"Sorry !! Wrong username or password !!\");</script> <a href='index.html#!/page_Schedule'>Click here</a> to return to login page");
}
?>
My problem is my website don't gave me response , when I enter email and pass the page is refresh to other page and the link became like this
http://localhost/myfolder/index.html?email1=user%40hotmail.com&pass1=123&login=Log+in#.php
Thank you for your response
remove these <form > </form> tag and you can see your action. your code will be like below
<fieldset style="width:355;">
<legend></legend>
<div id="Form">
<br><p><b> Log in</b></p>
<br>
</div>
<div class="col1 pad_left1">
<form name="logForm" onsubmit="return log()" action="log_exe.php" method="POST" >
<label style="font-size:100%;" >Email:</label> </br>
<input type="text" name="email1" placeholder="user#hotmail.com" id="email1" style="border:1px solid black; width:80%;" /> <b style="color:red;">*</b> </br><br>
<label style="font-size:100%;" > password:</label> </br>
<input type="password" name="pass1" placeholder="Password" id="pass1" style="border:1px solid black; width:80%;" /> <b style="color:red;">*</b>
<b style="display:none; color:red;" id="hide1">Sorry !! Wrong username or password</b>
</br>
<br>
<input style="font-size:120%;width:40%;padding:1%" type="submit" value="Log in" name="login" /></br><br>
</form>
</div>
</fieldset>
Remove the unwanted form from your index.html. ie modify the code like the following:
<fieldset style="width:355;">
<legend></legend>
<div id="Form">
<br><p><b> Log in</b></p>
<br>
</div>
<div class="col1 pad_left1">
<form name="logForm" onsubmit="return log()" action="log_exe.php" method="POST" >
<label style="font-size:100%;" >Email:</label> </br>
<input type="text" name="email1" placeholder="user#hotmail.com" id="email1" style="border:1px solid black; width:80%;" /> <b style="color:red;">*</b> </br><br>
<label style="font-size:100%;" > password:</label> </br>
<input type="password" name="pass1" placeholder="Password" id="pass1" style="border:1px solid black; width:80%;" /> <b style="color:red;">*</b>
<b style="display:none; color:red;" id="hide1">Sorry !! Wrong username or password</b>
</br>
<br>
<input style="font-size:120%;width:40%;padding:1%" type="submit" value="Log in" name="login" /></br><br>
</form>
</div>
</fieldset>

html-php form code not executing

I am trying to execute this html form code, the php script is not executing properly. Can you point out the problems?
<form action="form.php" method="post">
<p>
<input type="text" name="titles[]" /><br />
<textarea name="languages[]" ></textarea><br />
</p>
<p>
<input type="text" name="titles[]" /><br />
<textarea name="languages[]" ></textarea><br />
</p>
php script
<?php
if (isset($_POST['submit']))
{
$k=0;
foreach($_POST['languages'] as $language) {
echo $language = htmlentities($language);
echo $title = $_POST['titles'][$k];
$k++;
}
}
?>
HTML
<form action="form.php" method="post">
<p>
<input type="text" name="titles[]" /><br />
<textarea name="languages[]" ></textarea><br />
</p>
<p>
<input type="text" name="titles[]" /><br />
<textarea name="languages[]" ></textarea><br />
</p>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
PHP
<?php
if (isset($_POST['submit']))
{
foreach($_POST['languages'] as $key => $language) {
echo $language = htmlentities($language);
echo " -- ";
echo $title = $_POST['titles'][$key];
echo "<br/>";
}
}
?>

Categories