How to add a CSS class on HTML button via PHP - 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;

Related

Cannot find the page/object after click submit the form

I followed a online tutorial to connect my php code with MySQL. After I click the submit button, it said it cannot find the object (page). I did not see any code in my code? Any idea for me how to debug this code?
<?php
$user = 'root';
$pass = 'xxxxxx';
$db = 'testDB';
$db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect");
echo"Great work!";
$id ="";
$fname="";
$midname="";
$lname="";
function getPosts()
{
$posts =array();
$posts[0]=$_POST['Contact_ID'];
$posts[1]=$_POST['first_name'];
$posts[2]=$_POST['middle_name'];
$posts[3]=$_POST['last_name'];
return $posts;
}
//search
if(isset($_POST['search']))
{
$data = getPosts();
$search_Query="select * from Contact where contact_ID =$data[0]";
$search_Result=mysql_query($db, $search_Query);
if($search_Result)
{
if(mysql_num_rows($search_Result))
{
while($row=mysql_fetch_array($search_Result))
{
$id =$row['contact_ID'];
$fname =$row['first_name'];
$midname =$row['middle_name'];
$lname =$row['last_name_name'];
}
}
else
{
echo"No data for this Id";
}
}
else
echo"Result error";
}
?>
<!DOCTYPE Html>
<html>
<head>
<title>Contact Us</title>
</head>
<body>
<form action="php_insert_update_delete_search.php" method="post">
<input type="number", name="contact_ID" placeholder="Id" value="<?php echo $id;?>"><br><br>
<input type="text", name="first_name" placeholder="First Name" value="<?php echo $fname;?>"><br><br>
<input type="text", name="middle_name" placeholder="Middle Name" value="<?php echo $midname;?>"><br><br>
<input type="text", name="last_namename" placeholder="Last Name" value="<?php echo $lname;?>"><br><br>
<div>
<input type="submit" name="insert" value="Add">
<input type="submit" name="update" value="Update">
<input type="submit" name="search" value="Find">
</div>
</form>
</body>
</html>
remove php_insert_update_delete_search.php from the action of your form. The form is being redirected to php_insert_update_delete_search.php page when you click search button
<form action="" method="post">
<input type="number" name="contact_ID" placeholder="Id" value="<?php echo $id;?>"><br><br>
<input type="text" name="first_name" placeholder="First Name" value="<?php echo $fname;?>"><br><br>
<input type="text" name="middle_name" placeholder="Middle Name" value="<?php echo $midname;?>"><br><br>
<input type="text" name="last_namename" placeholder="Last Name" value="<?php echo $lname;?>"><br><br>
<div>
<input type="submit" name="insert" value="Add">
<input type="submit" name="update" value="Update">
<input type="submit" name="search" value="Find">
</div>
</form>
See You Have applied an action in your form tag . This action means that when ever a user will click on any button related to that form he or she will be redirected to that page (mentioned in action ) . So u need to make a page php_insert_update_delete_search.php and has to apply logic there for inserting the data .
If your wanting the form to go to another page to run code to process what you need
<form action="pageyouneed.php" method="post">
if you want to use the current page to parse your code:
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
$_SERVER["PHP_SELF"]; // Will return current document to parse code.

Results from search not shown in iframe (using switch statement)

We are using a switch statement to switch between languages but when we try to use case 1(the dutch/nl flag), the results no longer shows up. Everything works fine without using the switch case, however.
This is our index
<html>
<head>
<title>Welcome to Medispeak</title>
<link rel=stylesheet href=Medi.css>
</head>
<body>
<header>
<div class="lang">
<form method="GET" action="?set=lang">
<input id="vlag" name="lang" class="nl" type="submit" value="1">
<input id="vlag" name="lang" class="en" type="submit" value="2">
<input id="vlag" name="lang" class="du" type="submit" value="3">
<input id="vlag" name="lang" class="es" type="submit" value="4">
<input id="vlag" name="lang" class="pt" type="submit" value="5">
<input id="vlag" name="lang" class="fr" type="submit" value="6">
</form>
</div>
</header>
<h1>Zoeken op Medicijn</h1>
<form method="post" action="tabel.php" target="Mediframe">
Voer medicijn in:
<input type="text" name="zoek">
<br/>
<input type="submit" value="zoeken">
<input type="reset" value="wissen">
</form>
<div id="outer">
<div id="inner">
<iframe name="Mediframe" width="800" height="600" frameborder=0>
</iframe>
</div>
</div>
</body>
</html>
This is our search function:
<html>
<head>
<title>Medispeak</title>
<link rel=stylesheet href=ProjectCSS.css>
</head>
<body>
<?php
include 'db.php';
print_r($_REQUEST);
$zoek = $_REQUEST['zoek'];
$lang = $_REQUEST['lang'];
if ( empty ( $lang ) ) { $lang = "1"; }
switch ($lang) {
case "1":
echo 'case1';
try
{
$sQuery= "SELECT Medibijsluiter
FROM Medispeak
WHERE Medinaam LIKE ?";
$oStmt = $db->prepare($sQuery);
$oStmt->bindValue(1, "%$zoek%", PDO::PARAM_STR);
$oStmt->execute();
if($oStmt->rowCount()>0)
{
echo '<table border="2">';
echo '<thead>';
echo '<td>Medibijsluiter</td>';
echo '</thead>';
while($aRow = $oStmt->fetch(PDO::FETCH_ASSOC))
{
echo '<tr>';
echo '<td>'.$aRow['Medibijsluiter'].'</td>';
echo '</tr>';
}
echo '</table>';
}
else
{
echo 'Helaas,geen gegevens bekend';
}
break;
}
}
catch(PDOException $e)
{
$sMsg = '<p>
Regelnummer: '.$e->getLine().'<br />
Bestand: '.$e->getFile().'<br />
Foutmelding: '.$e->getMessage().'
</p>';
trigger_error($sMsg);
}
$db = null;
?>
</body>
</html>
You should use $_GET['lang'] instead of $_POST['lang']. Or you can user $_REQUEST['zoek'] and $_REQUEST['lang'].
If you have form type as GET, then the form data can be accessed using $_GET or $_REQUEST. If the type is POST, you should use either $_POST or $_REQUEST.
Read this to know more about GET, POST and REQUEST in PHP
So change your code
$zoek = $_POST['zoek'];
switch ($_GET['lang']) {
or
$zoek = $_REQUEST['zoek'];
switch ($_REQUEST['lang']) {
Updated Code
<html>
<head>
<title>Welcome to Medispeak</title>
<link rel=stylesheet href=Medi.css>
</head>
<body>
<?php
if(!empty($_REQUEST['zoek'])) {
$zoek = $_REQUEST['zoek'];
include 'db.php';
$lang = $_REQUEST['lang'];
if ( empty ( $lang ) ) { $lang = "1"; }
switch ($lang) {
case "1":
echo 'case1';
try
{
$sQuery= "SELECT Medibijsluiter FROM Medispeak WHERE Medinaam LIKE ?";
$oStmt = $db->prepare($sQuery);
$oStmt->bindValue(1, "%$zoek%", PDO::PARAM_STR);
$oStmt->execute();
if($oStmt->rowCount()>0)
{
echo '<table border="2"><thead><td>Medibijsluiter</td></thead>';
while($aRow = $oStmt->fetch(PDO::FETCH_ASSOC))
{
echo '<tr><td>'.$aRow['Medibijsluiter'].'</td></tr>';
}
echo '</table>';
} else {
echo 'Helaas,geen gegevens bekend';
}
break;
}
catch(PDOException $e) {
$sMsg = '<p>
Regelnummer: '.$e->getLine().'<br />
Bestand: '.$e->getFile().'<br />
Foutmelding: '.$e->getMessage().'
</p>';
trigger_error($sMsg);
}
}
$db = null;
}
?>
<header>
<div class="lang">
<form method="GET" action="?set=lang">
<input id="vlag" name="lang" class="nl" type="submit" value="1">
<input id="vlag" name="lang" class="en" type="submit" value="2">
<input id="vlag" name="lang" class="du" type="submit" value="3">
<input id="vlag" name="lang" class="es" type="submit" value="4">
<input id="vlag" name="lang" class="pt" type="submit" value="5">
<input id="vlag" name="lang" class="fr" type="submit" value="6">
</form>
</div>
</header>
<h1>Zoeken op Medicijn</h1>
<form method="post" action="" target="Mediframe">
Voer medicijn in:
<input type="text" name="zoek">
<br/>
<input type="submit" value="zoeken">
<input type="reset" value="wissen">
</form>
<div id="outer">
<div id="inner">
<iframe name="Mediframe" width="800" height="600" frameborder=0>
</iframe>
</div>
</div>
</body>
</html>
Update this code in index file. tabel.php content also added in this
file. So the form will be submitted to the same page and it will not
refer tabel.php.
You're using two different forms, one for language selection and one for searching. Thus, when submitting the second form whatever you selected in the first one gets completely ignored. (Additionally, the first form is using $_GET, and you're looking for the language in $_POST)
A quick fix would be moving your language selection element into the second form, so the selected language gets send along with the search phrase.
change switch ($_POST['lang']) to switch ($_GET['lang']) you mix up POST and GET with your two forms
<html>
<head>
<title>Welcome to Medispeak</title>
<link rel=stylesheet href=Medi.css>
</head>
<body>
<header>
<div class="lang">
<form method="GET" action="?set=lang">
<input id="vlag" name="lang" class="nl" type="submit" value="1">
<input id="vlag" name="lang" class="en" type="submit" value="2">
<input id="vlag" name="lang" class="du" type="submit" value="3">
<input id="vlag" name="lang" class="es" type="submit" value="4">
<input id="vlag" name="lang" class="pt" type="submit" value="5">
<input id="vlag" name="lang" class="fr" type="submit" value="6">
</form>
</div>
</header>
<form method="post" action="tabel.php" target="Mediframe">
<h1>Zoeken op Medicijn</h1>
Voer medicijn in:
<input type="text" name="zoek">
<br/>
<input type="submit" value="zoeken">
<input type="reset" value="wissen">
<input id="vlag" type="hidden" name="lang" class="nl" value="<?=$_GET["lang"]?>">
</form>
<div id="outer">
<div id="inner">
<iframe name="Mediframe" width="800" height="600" frameborder=0>
</iframe>
</div>
</div>
</body>
</html>
tabel.php
<html>
<head>
<title>Medispeak</title>
<link rel=stylesheet href=ProjectCSS.css>
</head>
$zoek = $_POST['zoek'];
switch ($_POST['lang']) {
case "1":
try {
$sQuery = "SELECT Medibijsluiter
FROM Medispeak
WHERE Medinaam LIKE ?";
$oStmt = $db->prepare($sQuery);
$oStmt->bindValue(1, "%$zoek%", PDO::PARAM_STR);
$oStmt->execute();
if ($oStmt->rowCount() > 0) {
echo '<table border="2">';
echo '<thead>';
echo '<td>Medibijsluiter</td>';
echo '</thead>';
while ($aRow = $oStmt->fetch(PDO::FETCH_ASSOC)) {
echo '<tr>';
echo '<td>' . $aRow['Medibijsluiter'] . '</td>';
echo '</tr>';
}
echo '</table>';
} else {
echo 'Helaas,geen gegevens bekend';
}
} catch
(PDOException $e) {
$sMsg = '<p>
Regelnummer: ' . $e->getLine() . '<br />
Bestand: ' . $e->getFile() . '<br />
Foutmelding: ' . $e->getMessage() . '
</p>';
trigger_error($sMsg);
}
$db = null;
break;
}
?>
</body>
</html>
But you can do it more cleaner. you should use a select imput within your second form :)

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

css for dynamic rows not working

<html>
<head>
<title>truck page</title>
<style type="text/css">
tr {
padding: 4px;
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$db = new mysqli("localhost", "root", "", "db1");
function getdata($form_element_name) {
$var = strip_tags($_GET[$form_element_name]);
return $var;
}
?>
<fieldset>
<legend> add a new truck </legend>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="GET">
<label>truck number <input type="text" name="truck_number" /></label>
<label>owner name <input type="text" name="truck_owner_name" /></label>
<label>owner phone <input type="text" name="truck_owner_ph" maxlength="10" /></label>
<input type="submit" name="truck_add" value="add" />
<input type="reset" />
</form>
<?php if(isset($_GET['truck_add'])) {
$truck_number = getdata('truck_number');
$truck_owner_name = getdata('truck_owner_name');
$truck_owner_ph = getdata('truck_owner_ph');
$sql_truck_add = "INSERT INTO truck (truck_number, truck_owner_name, truck_owner_ph)
VALUES ('$truck_number', '$truck_owner_name', '$truck_owner_ph' )";
$result_truck_add = $db->query($sql_truck_add);
header("Location: http://localhost/bkp/truckpage.php");
} ?>
</fieldset>
<table>
<tr>
<th>truck number</th>
<th>owner name</th>
<th>phone</th>
<th>operation</th>
<th></th>
</tr>
<?php
$sql_truck_retrieve = "SELECT * from truck";
$result_truck_retrieve= $db->query($sql_truck_retrieve);
while($row=$result_truck_retrieve->fetch_assoc()) {
$truck_num_current = $row['truck_number']; ?>
<tr id="<?php echo "{$truck_num_current}"; ?>">
<td><?php echo "{$row['truck_number']}"; ?></td>
<td><?php echo "{$row['truck_owner_name']}"; ?></td>
<td><?php echo "{$row['truck_owner_ph']}"; ?></td>
<td>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
<input type="hidden" name="truck_numcur" value="<?php echo "{$row['truck_number']}"; ?>" />
<input type="submit" name="truck_update" value="update" />
<input type="submit" name="truck_delete" value="delete" />
</form>
</td>
<td>
<?php // action to be taken if either update or delete is selected
if(isset($_GET['truck_update'])) {
$truck_numcur = $_GET['truck_numcur'];
if($truck_numcur == $truck_num_current) { ?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="GET">
<input type="hidden" name="truck_update_num" value="<?php echo "{$truck_numcur}"; ?>" />
<input type="text" name="truck_num_update" placeholder="truck number" />
<input type="text" name="owner_name_update" placeholder="owner name" />
<input type="text" name="owner_phone_update" placeholder="owner phone" />
<input type="submit" value="save" name="truck_update_yes" />
<input type="submit" value="cancel" name="truck_update_no" />
</form>
<?php }
}
else {
if(isset($_GET['truck_delete'])) {
$truck_numcur = $_GET['truck_numcur'];
if($truck_numcur == $truck_num_current) {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="GET">
<span>confirm? </span>
<input type="hidden" name="truck_del_num" value="<?php echo "{$truck_numcur}"; ?>" />
<input type="submit" name="truck_delete_yes" value="yes" />
<input type="submit" name="truck_delete_no" value="no" />
</form>
<?php }
}
}
//
// action to be taken after delete confirmation
if(isset($_GET['truck_delete_yes'])) {
$truck_del_num = $_GET['truck_del_num'];
$sql_truck_del = "DELETE from truck WHERE truck_number = '{$truck_del_num}'";
$result_truck_del = $db->query($sql_truck_del);
header("Location: http://localhost/bkp/truckpage.php");
}
else {
if(isset($_GET['truck_delete_no'])) {
header("Location: http://localhost/bkp/truckpage.php");
//---------------------------------------------------------------------------
//header("Location: http://localhost/bkp/truckpage.php/#$truck_num_current");
//---------------------------------------------------------------------------
}
}
//
// action to be taken after update confirmation
if(isset($_GET['truck_update_yes'])) {
$truck_update_num = $_GET['truck_update_num'];
$truck_num_update = getdata('truck_num_update');
$owner_name_update = getdata('owner_name_update');
$owner_phone_update = getdata('owner_phone_update');
$sql_truck_update = "UPDATE truck SET truck_number = '$truck_num_update', truck_owner_name = '$owner_name_update', truck_owner_ph = '$owner_phone_update' WHERE truck_number = '{$truck_update_num}' ";
$result_truck_update = $db->query($sql_truck_update);
header("Location: http://localhost/bkp/truckpage.php");
}
else {
if(isset($_GET['truck_update_no'])) {
header("Location: http://localhost/bkp/truckpage.php");
}
}
//
?>
</td>
</tr>
<?php } ?>
</table>
</body>
I am trying to extract values from a db table and add them as table rows. The rows are being shown correctly but when I tried to add css styles for the table rows it did not work. I tried to give a border and padding.I am new to web development and cannot understand why the css part is not working.Can anyone please tell how to make the css part work in this php code?
The problem lies in that you cannot very effectively style tr elements. You will need to target the parent table or the child td elements to get the affect you're looking for.
Check out this JSFiddle example: http://jsfiddle.net/v6zNt/
CSS:
table {
border: 1px solid #000;
}
/* notice how these styles does not apply anywhere */
tr {
border: 1px solid green;
padding: 4px;
}
td {
border: 1px solid red;
padding: 4px;
}
HTML:
<table>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</table>
You can style tables, it's just that they have their own set of rules that you need to be mindful of. Here's a great introduction: http://css-tricks.com/complete-guide-table-element/

PHP $_POST returns null values

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;
}
}
?>

Categories