PHP textarea problem, $_POST returns empty array - php

I already read a lot of google results about my problem and any methods don't help me. print_r($_POST) returns me empty array, i try fix this for two days. I was try really a lot of methods, from stackoverlow, from other websites and any from this methods don't work for me. $_POST is empty array.
My code
<?php
require_once 'config.php';
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function updateRec($tbl, $index) {
if($GLOBALS['connection'] !== null) {
$connection = $GLOBALS['connection'];
if($tbl == 'users') {
//if(isset($_POST['id']) && isset($_POST['username']) && isset($_POST['password']) && isset($_POST['rank'])) {
$id = $_POST['id'];
$login = test_input($_POST['username']);
$pass = password_hash(test_input($_POST['password']), PASSWORD_DEFAULT);
$rank = test_input($_POST['rank']);
$q = "UPDATE $tbl SET id='$id', username='$login', password='$pass', rank='$rank' WHERE id='$index'";
echo "<script type='text/javascript'>alert(".$login.");</script>";
//}
}
//mysqli_query($connection, $q);
header('location: '.$tbl.'.php?page=1');
}
}
function createForm($a) {
if ($a === 'edit') {
echo "<center><div id='editForm'>";
$q = "DESCRIBE ".$_GET['tbl']."";
$qr = mysqli_query($GLOBALS['connection'], $q);
while ($rw = mysqli_fetch_array($qr)) {
echo "<tr>";
echo "<td> " . $rw['Field'] . "</td><br/>";
echo "<td><textarea rows='4' cols='50' name='".$rw['Field']."' required></textarea></td>";
echo "<br/>";
echo "</tr>";
}
echo '<form action="forms.php?action=saveE&tbl='.$_GET['tbl'].'&row='.$_GET['row'].'" method="post" accept-charset="utf-8" style="transform: translate(5vw, -20vh); height: 0vh;">';
echo '<button id="fButton" name="submit" style="">Zapisz</button></form>'. '<button id="fButton" style="transform: translate(5vw, 8vh); margin-left: 6%;">Anuluj</button>';
print_r($_POST);
echo "</div></center>";
};
if ($a === 'saveE') {
updateRec($_GET['tbl'], $_GET['row']);
};
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<title>Control Panel</title>
<link rel="stylesheet" type="text/css" href="global.css">
<meta charset="utf-8" name="viewport" content="width=device.width, initial-scale=1">
</head>
<body>
<header>
<p>Admin Control Panel</p>
</header>
</body>
</html>
My textarea
while ($rw = mysqli_fetch_array($qr)) {
echo "<tr>";
echo "<td> " . $rw['Field'] . "</td><br/>";
echo "<td><textarea rows='4' cols='50' name='".$rw['Field']."' required></textarea></td>";
echo "<br/>";
echo "</tr>";
}
I was too try use id instead name and it don't help me.

Related

Multiplication Table PHP

I wanted to create a multiplication table with a custom function and also get the number of rows and columns from the user, and I want if each row was an even number then its field would be red (background) and if it was odd number it would have a green background and i also write some codes but i'm not sure if it's true or not:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<form method="post">
<input type="number" name="rows" placeholder="Rows">
<input type="number" name="columns" placeholder="Columns">
<button type="submit" name="button">
Create
</button>
</form>
<table border="1px">
<?php
if (isset($_POST["button"])) {
$userRows = $_POST["rows"];
$userColumns = $_POST["columns"];
function multiplicationTable($rows, $columns)
{
$zarb = $rows * $columns;
return $zarb;
}
$x = 1;
while ($x <= $userRows) {
echo "<tr>";
$y = 1;
while ($y <= $userColumns) {
if ($x % 2 == 0) {
echo "<td style='background-color: red;'>" . multiplicationTable($x, $y) . "</td>";
} else {
echo "<td style='background-color: green;'>" . multiplicationTable($x, $y) . "</td>";
}
$y++;
}
$x++;
echo "</tr>";
}
}
?>
</table>
</body>
</html>
How about changing the while loops to this:
while ($x <= $userRows) {
echo "<tr>";
$y = 1;
while ($y <= $userColumns) {
$val = multiplicationTable($x, $y);
if ($val % 2 == 0) {
echo "<td style='background-color: red;'>" . $val . "</td>";
} else {
echo "<td style='background-color: green;'>" . $val . "</td>";
}
$y++;
}
$x++;
echo "</tr>";
}
Well, your code looks good. Click here to see how it looks like...
the final result is in this link, and even if some of the numbers are even, they're still displayed in green and i don't know what should i do
https://imgur.com/D1Sweee

Saving quiz results to database

I have created a multiple choice quiz/questionnaire. The questions and answers are pulling from the database and I have sessions setup on the webpage. At the minute the user can only print the webpage when they get their results.
I am using phpmyadmin and php for the quiz. I am currently pulling from one database table called questions with the fields, questionID, question, ansYes and ansNo.
<?
//Always start this first
session_start();
include ("dbConnect.php");
if ( isset( $_SESSION['user_email'] ) ) {
//Grab user data from the database using user email
} else {
// Redirect them to the login page
header("login.php");
}
$sql = "SELECT * FROM questions";
$dbQuery = $db->prepare($sql);
$dbQuery->execute();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>List</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<style>
/* CSS animation */
#-webkit-keyframes fadeIn {
0% {
opacity:0;
transform: scale(0.6);
}
100% {
opacity:100%;
transform: scale(1);
}
}
#keyframes fadeIn {
0% { opacity:0; }
100% { opacity:100%; }
}
</style>
<?php include("navIn.php"); ?>
<body>
<div class="container-fluid">
<br>
<?php
/*Printing out questions*/
if(!isset($_POST["submitForm"])) {
echo "<form method='post' action='checklist.php'>";
if (isset($_SESSION["first_name"])) {
echo '<span style="color:#000000;position:left; left:1060px;">Hello '.$_SESSION["first_name"].'!<br></span>';
}
while($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
echo"<div class=container-fluid text-center>";
echo "<h4>" . $dbRow["question"] . "</h4>";
echo "<h4><input type='radio' required name='" . $dbRow["questionID"] . "' value='Yes'> Yes <br></h4?";
echo "<h4><input type='radio' required name='" . $dbRow["questionID"] . "' value='No'> No <br><br></h4>";
echo "</div>";
}
echo "<input type='submit' name='submitForm' value='Submit'>";
echo "</form>";
echo"</div>";
} else {
if (isset($_SESSION["first_name"])) {
echo '<span style="color:#000000;position:left; left:1060px;">Hello '.$_SESSION["first_name"].'!<br></span>';
}
/*printing out results from questions */
echo"<div class=container-fluid text-center>";
while($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
if($_POST[$dbRow["questionID"]] == "Yes") {
echo '<h4 span style="color:blue;"> '.$dbRow["question"].'</span>';
/*echo "<h4>" .$dbRow["question"]."<br></h4><br>";*/
echo "<h4>" . $dbRow["ansYes"] . "</h4><br>";
} else {
echo '<h4 span style="color:blue;"> '.$dbRow["question"].'</span>';
/* echo "<h4>" .$dbRow["question"]."<br></h4><br>";*/
echo "<h4>" . $dbRow["ansNo"] . "</h4><br>";
}
}
echo"Click the button bellow to print this page!";
echo'<br><form> <input type=button value="Print me!" onClick="javascript:window.print()"> </form>';
echo"</div>";
}
?>
</div>
</body>
<?php include ("footer.php"); ?>
</html>
I would like to be able to save the users results so anytime they access checklist.php their results are there instead of having to print the page out.

Adding a counter to the table

I'm trying to add a counter to the table in the following code. but I couldn't be successful. Can I get a little help, please? thanks. Something like this:
$counter = 0;
$counter++;
if($counter % 33 == 0)
So that when the counter is added, table will continue after %33 on the right of the page, and it will continue like that, instead of going down the page.
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="etc/sumain.css" />
</head>
<body>
<table class="tbresult">
<?php
include ("confige.php");
$query = 'select * from employees';
$result = mysqli_query($link, $query);
if (!$result) {
$message = 'ERROR:' . mysqli_error($link);
return $message;
} else {
$i = 0;
echo '<form name="select" action="" method="GET">';
echo '<select name="mySelect" id="mySelect" onchange="this.form.submit()">';
while ($i < mysqli_field_count($link)) {
$meta =
mysqli_fetch_field_direct($result, $i);
echo '<option>' . $meta->name . '</option>';
$i = $i + 1;
}
echo '</select>';
echo '</form>';
}
if(isset($_GET['mySelect'])) {
$myselect = $_GET['mySelect'];
$sql = "SELECT `$myselect` as mySelect FROM employees"; // add column alias
$result = mysqli_query($link, $sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["mySelect"] . "</td></tr>";
}
echo "</table>";
}
}
mysqli_close($link);
?>
</body>
</html>
First, don't have an HTML form inside a table, this is not valid, AFIK, and can cause you many troubles in different browsers.
You need simply open the table once, then create a counter = 0 and on each while loop add 1 to it. Then check, if it divides by 33 than you close the table and open a new one. After the loop you close the last table.
The side by side alignment can be done with CSS, something like .tbresult {float: left; width: 200px;}
Something like this:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="etc/sumain.css" />
</head>
<body>
<?php
include ("confige.php");
$query = 'select * from employees';
$result = mysqli_query($link, $query);
if (!$result) {
$message = 'ERROR:' . mysqli_error($link);
return $message;
} else {
$i = 0;
echo '<form name="select" action="" method="GET">';
echo '<select name="mySelect" id="mySelect" onchange="this.form.submit()">';
while ($i < mysqli_field_count($link)) {
$meta =
mysqli_fetch_field_direct($result, $i);
echo '<option>' . $meta->name . '</option>';
$i = $i + 1;
}
echo '</select>';
echo '</form>';
}
if(isset($_GET['mySelect'])) {
$myselect = $_GET['mySelect'];
$sql = "SELECT `$myselect` as mySelect FROM employees"; // add column alias
$result = mysqli_query($link, $sql);
if ($result->num_rows > 0) {
// output data of each row
$table_row_counter = 0;
echo '<table class="tbresult">';
while($row = $result->fetch_assoc())
{
$table_row_counter++;
if ($table_row_counter % 33 == 0) {
echo '</table>';
echo '<table class="tbresult">';
}
echo "<tr><td>" . $row["mySelect"] . "</td></tr>";
}
}
}
mysqli_close($link);
?>
</body>
</html>

Why is PHP not displaying all values correctly?

Unfortunately my code isn't working quite well.
The source code:
<!DOCTYPE html>
<html>
<head>
<title>Query data from News database and display in table</title>
<meta charset="UTF-8">
<meta name="description" content="" />
<meta name="author" content="WRBikAir" />
<meta name="keywords" content="" />
<style>
table, th, td{
border: 1px solid black;
}
</style>
</head>
<body>
<?php
error_reporting(E_ALL);
echo "Test 1";
$con = mysqli_connect("mysql.hostinger.com","u441817146_admin","CBGApp","u441817146_cbg");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM News";
if ($result = mysqli_query($con, $sql)) {
echo "<table>";
while($row = $result->fetch_assoc()) {
$r = json_encode($row);
echo "<tr><td>" . $r['NID'] . "</td><td>" . $r['headline'] . "</td><td>" . $row['text'] . "</td><td>" . $r['timestamp'] . "</td></tr>";
}
echo "</table>";
} else {
echo "no result.";
}
mysqli_close($con);
echo "2";
?>
</body>
</html>
Everything works fine, except of the output of the NID, headline and timestamp. There are all '{'. Does it mean, that there is now value? because if I simply print them out (encoded of course) there are values e.g.:
{"NID":"1","headline":"Testartikel 2","text":"test test test","timestamp":"15.11.2017, 18:13"}
Does somebody knows a solution?
You are using $result 2 times on $result = mysqli_query($con, $sql) and in your foreach loop. I changed the one in the foreach loop to $results instead of $result.
Also try turning on error reporting using:
error_reporting(E_ALL);
Try using:
<!DOCTYPE html>
<html>
<head>
<title>Query data from News database and display in table</title>
<meta charset="UTF-8">
<meta name="description" content="" />
<meta name="author" content="WRBikAir" />
<meta name="keywords" content="" />
<style>
table, th, td{
border: 1px solid black;
}
</style>
</head>
<body>
<?php
echo "Test 1";
$con = mysqli_connect(CENSORED (but working in other classes fine);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM News";
if ($result = mysqli_query($con, $sql)) {
$resultArray = array();
$tempArray = array();
while($row = $result->fetch_object()) {
$tempArray = $row;
array_push($resultArray, $tempArray);
}
echo "<table>";
foreach ($resultArray as $results) {
$r = json_encode($results);
echo "<tr><td>" . $results['headline'] . "</td><td>" . $results['text'] . "</td></tr>";
}
echo "</table>"
}
mysqli_close($con);
echo "2";
?>
</body>
</html>
For everybody who need the working answer.
Thank you to MasterOfCoding, GrumpyCrouton, Paul Spiegel and prodigitalson.
Special thanks to tadman for the insider information.
Now the code:
<!DOCTYPE html>
<html>
<head>
<title>Query data from News database and display in table</title>
<meta charset="UTF-8">
<meta name="description" content="" />
<meta name="author" content="WRBikAir" />
<meta name="keywords" content="" />
<style>
table, th, td{
border: 1px solid black;
}
</style>
</head>
<body>
<?php
error_reporting(E_ALL);
echo "Test 1";
$con = mysqli_connect("...","...","...","...");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM News";
if ($result = mysqli_query($con, $sql)) {
echo "<table>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row['NID'] . "</td><td>" . $row['headline'] . "</td><td>" . $row['text'] . "</td><td>" . $row['timestamp'] . "</td></tr>";
}
echo "</table>";
} else {
echo "no result.";
}
mysqli_close($con);
echo "2";
?>
</body>
</html>

How can I make my submit button add the values to my cart table

I'm trying to get the "Cart_View02" table to update when the submit button is clicked. I'm new to php and sql this semester. Anybody know what I'm doing wrong?
<?PHP
if (isset($_GET['tab'])) $table= $_GET['tab'];
else $table="Cart_view02";
$reset = true;
$errmsg = array("","");
//1. Make a connection to the database
$dbconn = mysqli_connect("localhost","root","","mydatabase1")
or die(mysqli_connect_error());
if($_SERVER['REQUEST_METHOD']=='POST'){
$action = $_POST['action'];
$qty = $_POST['txtqty'];
$crtnum = $_POST[100];
$itemid = $_POST[$row[0]];
if($action == "select"){
$sql = "INSERT INTO cart_lineitems VALUES ($crtnum, '$itemid',
'$qty');";
$result = mysqli_query($db,$sql) or die(mysql_error($db));
}
//print_r($_POST);
header("location: cartdump.php");
}
?>
<!DOCTYPE html>
<html>
<head><title>Order Page</title>
<link href="css/styles.css" type="text/css" rel="stylesheet" />
<script type="text/javascript">
</script>
<img class="banner" src="Images/acme_banner.jpg" alt="Acme Spook Shoppe"/>
</head>
<body>
<br><br>
Home
<form name="form1" method='POST'>
<?php
if (isset($_GET['prod'])) $prod=$_GET['prod'];
else $prod = "arm01";
//echo $prod; //<------To print results
$sql = "select productid,name,imagefile, "
."shortdescription, longdescription,"
."unitprice "
."from products "
."where productid='$prod'";
echo "<br>";
//echo $sql; //<------To print results
//2. Run a query against the database
$result = mysqli_query($dbconn,$sql)
or die(mysqli_error($dbconn));
//print_r($result); //<------To print results
//3. Return the results
echo "<table class=ordertable >";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[1]<br><br>";
echo "$row[3]<br><br>";
echo "$row[4]<br><br>";
echo "Price: $row[5]<br><br>";
echo "<input type='text' name='txtqty' value='1' size=2 maxlength=2>";
echo "<input type='submit' name='btnadd' value='Add To Cart'
OnClick='SubmitForm'>";
echo "</td>";
echo "<td><img src='images/products/$row[2]' "
."height=300px width =250px></td>";
echo "</tr>";
echo "</table>";
//print_r($row);
}
//4. Release the resources
mysqli_free_result($result);
//5. Close the connection
mysqli_close($dbconn);
?>
</form>
</body>
</html>
Any help on this would be greatly appreciated.
This is what I ended up with:
<!DOCTYPE html>
<?PHP
print_r($_POST);
if (isset($_GET['prod'])) $prod=$_GET['prod'];
else $prod = "arm01";
//if (isset($_GET['tab'])) $table= $_GET['tab'];
// else $table="Cart_view02";
//$reset = true;
//$errmsg = array("","");
//1. Make a connection to the database
$dbconn = mysqli_connect("localhost","root","","mydatabase1")
or die(mysqli_connect_error());
if($_SERVER['REQUEST_METHOD']=='POST'){
$qty = $_POST['txtqty'];
$crtnum = 100;
if (preg_match("/^[0-9]{1,2}$/",$qty)){
$sql = "INSERT INTO cart_lineitems VALUES ($crtnum, '$prod', $qty);";
$result = mysqli_query($dbconn,$sql) or die(mysqli_error($dbconn));
//echo"<br>$sql";
header("location: cartdump.php");
}
else echo "Quantity is not valid<br>";
}
?>
<html>
<head><title>Order Page</title>
<link href="css/styles.css" type="text/css" rel="stylesheet" />
<script type="text/javascript">
</script>
<img class="banner" src="Images/acme_banner.jpg" alt="Acme Spook Shoppe"/>
</head>
<body>
<br><br>
Home
<form name="form1" method='POST'>
<?php
//echo $prod; //<------To print results
$sql = "select productid,name,imagefile, "
."shortdescription, longdescription,"
."unitprice "
."from products "
."where productid='$prod'";
echo "<br>";
//echo $sql; //<------To print results
//2. Run a query against the database
$result = mysqli_query($dbconn,$sql)
or die(mysqli_error($dbconn));
//print_r($result); //<------To print results
//3. Return the results
echo "<table class=ordertable >";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[1]<br><br>";
echo "$row[3]<br><br>";
echo "$row[4]<br><br>";
echo "Price: $row[5]<br><br>";
echo "<input type='text' name='txtqty' value='1' size=2 maxlength=2>";
echo "<input type='submit' name='btnadd' value='Add To Cart' OnClick='SubmitForm'>";
echo "</td>";
echo "<td><img src='images/products/$row[2]' "
."height=300px width =250px></td>";
echo "</tr>";
echo "</table>";
//print_r($row);
}
//4. Release the resources
mysqli_free_result($result);
//5. Close the connection
mysqli_close($dbconn);
?>
</form>
</body>
</html>

Categories