HTML form WON'T POST data ($_POST[] is empty) - php

One of my html forms is not POSTing data, the submit button respect the action attribute and redirect the page as spected BUT $_POST[] is empty for some reason. could sonebody help me find the issue here?
I already tried using html method POST and even ajax to process the form and submit the data, and theres no case. $_POST[] will allways stay empty (this is the only page that does that, I hava an exact copy of the form but without the GET method and it works fine...
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="nocep-rules.css" rel="stylesheet" type="text/css">
<?php
session_start();
$link = mysqli_connect("localhost", "xxxx", "xxxx", "xxxxxx");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
<?php
if(!isset($_SESSION['logged'])){
header("location: /bubale/login.php");
}
?>
</head>
<body>
<div class="header">
<img class="logo" src="/bubale/img/logo1.png" alt="" />
<img class="foro-title" src="/bubale/img/loja_title.png" alt="" />
</div>
<div class="menu">
<nav class="site-nav">
<ul>
<li>HOME</li>
<li>LOJA</li>
<li>FORUM</li>
<li>CHECKOUT</li>
<li>SAIR</li>
</ul>
</nav>
</div>
<div class="info">
<table width="100%">
<tr>
<td width="20%">
<h2 class="text">USUÁRIO:</h2>
<?php echo $_SESSION['logged']; ?>
<h2 class="text2">PONTOS:</h2>
<?php
$sql5= "Select points from profile where user = '".$_SESSION['logged']."'";
$result5 = mysqli_query($link, $sql5);
if(mysqli_num_rows($result5)==1){
while($row5 = mysqli_fetch_assoc($result5)) {
echo $row5['points'];
}
}
?>
</td>
<td width="30%">
<?php
$sql2= "select * from bolsinhas where user = '".$_SESSION['logged']."' && rev_id = '0'";
$result2 = mysqli_query($link, $sql2);
$count = mysqli_num_rows($result2);
?>
<h2 class="text">BOLSINHAS:</h2>
<?php echo $count; ?>
<h2 class="text2">PRODUTOS:</h2>
<?php
$sql3= "select * from fullsize where user = '".$_SESSION['logged']."' && rev_id = '0'";
$result3 = mysqli_query($link, $sql3);
$count3 = mysqli_num_rows($result3);
echo $count3;
?>
</td>
<td>
<?php
if(!empty($_SESSION["shopping_cart"])) {
$cart_count = count(array_keys($_SESSION["shopping_cart"]));
}else{$cart_count = "0";}
?>
<img id="icon1" src="img/basket.png" title="Compras" ><span id = "count_wrapper"><a id="count">
<?php echo $cart_count; ?></a></span>
<img class="icon" src="img/sale.png" title="Promoção" alt="" >
<img class="icon" src="img/points.png" title="Fidelidade Buba'le" alt="" >
</td>
</table>
</div>
<h1 class="frete">FRETE:</h1>
<div class="wrapper_frete">
<?php
$rua1 = $_GET['1'];
$numero1 = $_GET['2'];
$apt1 = $_GET['3'];
$cep1 = $_GET['4'];
?>
<table id="forma_frete" width="100%">
<tbody>
<th>DADOS PESSOAIS</th>
<th>OOPS!!!</th>
<tr>
<form id="modform" method="POST" action="nocep2.php">
<td width="40%" class="td2">
<table id="nested" width="100%">
<tr>
<td id="td-nested-left" width="30%">
<label class="label-form" for="user">USUÁRIO:</label><br>
<label class="label-form" for="rua">RUA:</label><br>
<label class="label-form" for="numero">NÚMERO:</label><br>
<label class="label-form" for="apt">APT:</label><br>
<label class="label-form" for="cep">CEP:</label><br>
</td>
<td id="td-nested-right">
<input name="user" form="buttons" type="text" maxlenght="20" disabled value="<?php echo $_SESSION['logged']; ?>" /><br>
<input name="rua" form="buttons" type="text" value="<?php echo $rua1; ?>" /><br>
<input name="numero" form="buttons" type="text" value="<?php echo $numero1;?>" /><br>
<input name="apt" form="buttons" type="text" value="<?php echo $apt1; ?>" /><br>
<input name="cep" form="buttons" type="text" value="<?php echo $cep1;?>" />
</td>
</tr>
</table>
<input type="submit" class="refresh" value="ATUALIZAR">
</td>
</form>
<td id="td_mensage">
<h3 id="mensage">-irrelevant text-. </h3>
</td>
</tr>
<tr>
<td id="td_ob" colspan="2">
<h3 id="comentario">OBSERVAÇAÕ:</h3>
<textarea form="buttons" name="observacion" id="text_ob" placeholder="Deixe aqui sua observaçaõ..."></textarea>
</td>
</tr>
</tbody>
</table>
</div>
<h1 class="frete">SUA COMPRA:</h1>
<?php
if(isset($_SESSION["shopping_cart"])){
$total_price = 0;
?>
<?php } ?>
<table class="table">
<tbody>
<tr>
<th></th>
<th>PRODUTO</th>
<th>QUANTIDADE</th>
<th>VALOR UNITARIO</th>
<th>VALOR TOTAL</th>
</tr>
<?php
foreach ($_SESSION["shopping_cart"] as $product){
?>
<tr>
<td id="imgtd"><img src='<?php echo $product["image"]; ?>' width="90" height="80" /></td>
<td><?php echo $product["name"]; ?><br />
<form method='post' action=''>
<input type='hidden' name='code' value="<?php echo $product["code"]; ?>" />
</form></td>
<td><form method='post' action=''>
<input type='hidden' name='code2' value="<?php echo $product["code"]; ?>" />
<?php echo $product["quantity"];?>
</form></td>
<td><?php echo "R$".$product["price"]; ?></td>
<td><?php echo "R$".$product["price"]*$product["quantity"]; ?></td>
</tr>
<?php
$total_price += ($product["price"]*$product["quantity"]);
}
?>
<tr>
<td class="td_tot" colspan="5" align="right"><strong id="total">TOTAL: <?php echo "R$".$total_price; ?></strong></td>
</tr>
</tbody>
</table>
<form method="post" action="contact-venta.php" id="buttons">
<?php
?>
<input type="hidden" name="compras" value="<?php echo print_r($_SESSION["shopping_cart"], TRUE); ?>"/>
<input type="hidden" name="total" value="<?php echo $total_price; ?>">
<input type="submit" id="submit" value="ENVIAR">
</form>
<?php mysqli_close($link); ?>
</body>
</html>
the issue is with the first submit (the one with the value ACTUALIZAR), the form will go to nocep2.php BUT $_POST[] will remain empty .... I posted the entire page just in case there is an issue I'm not seeing....

Your HTML is rather confused.
Firstly, <form> is not a valid element in <tr>. The browser is rendering them outside the table, and separate from your <input>. Move your form tags inside the <td>, or move them outside the <table>
Eg
<table>
<tr>
<td>
<form method="POST">
<input type="text" name="inputName">
<input type="submit">
</form>
</td>
</tr>
</table>
Or, if you need to spread your form over several cells:
<form method="POST">
<table>
<tr>
<td>
<input type="text" name="inputName1">
</td>
<td>
<input type="text" name="inputName2">
</td>
<td>
<input type="submit">
</td>
</tr>
</table>
</form>
Secondly, you're creating a number of <input> elements of type "hidden", each with their own form tags, so they are each part of a different form. Reorder your code so that the hidden elements appear within the same <form> as the rest of your <input>

Related

Need to send an array in PHP

I'm trying to send multiples values to a PHP form.
This is the form that I use to send values:
<form id="form1" name="form1" method="POST" action="../WSweb/Factura.php">
<table width="561" height="79" border="1">
<tr>
<td width="30%" height="32">ProductID</td>
<td width="30%" height="32">SKU</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_test1['ProductID']; ?>
<input
type="hidden"
name="Product[id][ProductID]"
value="<?php echo $row_test1['ProductID']; ?>"
/>
</td>
<td><?php echo $row_test1['SKU']; ?>
<input
type="hidden"
name="Product[id][SKU]"
value="<?php echo $row_test1['SKU']; ?>"
/>
</td>
</tr>
<?php } while ($row_test1 = mysqli_fetch_assoc($test1)); ?>
</table>
<input type="submit" value="Update" name="Facturar">
</form>
And this is the action file:
if(isset($_POST['Update']))
{
$ProductID=$_POST['Product'];
print_r(json_encode($ProductID));
}
The problem that I have is when I send multiple values, for example the below table:
ProductID SKU
103 WH004BI
137 VO007BI
I alway get this result:
{"id":{"ProductID":"137","SKU":"VO007BI"}}
When I actually want to get a result like this:
{"id":[{"ProductID":"103","SKU":"WH004BI"},{"ProductID":"137","SKU":"VO007BI"}]}
You're going to want to do something like this:
<form id="form1" name="form1" method="POST" action="../WSweb/Factura.php">
<table width="561" height="79" border="1">
<tr>
<td width="30%" height="32">ProductID</td>
<td width="30%" height="32">SKU</td>
</tr>
<?php $i = 0; ?>
<?php while ($row_test1 = mysqli_fetch_assoc($test1) { ?>
<tr>
<td>
<?php echo $row_test1['ProductID']; ?>
<input
type="hidden"
name="Product[id][<?= $i; ?>][ProductID]"
value="<?php echo $row_test1['ProductID']; ?>"
/>
</td>
<td>
<?php echo $row_test1['SKU']; ?>
<input
type="hidden"
name="Product[id][<?= $i; ?>][SKU]"
value="<?php echo $row_test1['SKU']; ?>"
/>
</td>
</tr>
<?php $i++; ?>
<?php } ?>
</table>
<input type="submit" value="Update" name="Facturar">
</form>
Note that I have put a $i = 0 at the start of the loop, and $i++ at the end of the loop.
Additionally, I have changed the names to the following:
name="Product[id][<?= $i; ?>][SKU]"
Which will prevent the issue you were having in the comment section regarding an ill-formed array.

show posted data after submission with all old submission

I have a form, which have some input fields and also have some check box fields. If any one fill fields and select some checkbox and then submit. Then some data according submission shows on another div.
Now If same person again fill that fields after first submission and select checkbox and submit. Then some data according submission shows on div. But it replace old submission content. I need all submission content, on div including all old submission content.
My code is:
<form class="reservation-form mb-0" action="" method="post" novalidate autocomplete="off">
<input name="name1" class="form-control required " type="text" placeholder="Enter Name" aria-required="true" required>
<input name="age" class="form-control required " type="number" placeholder="Enter Age" aria-required="true" required>
<select required id="selectarea" class="form-control" name="selectgender">
<option>Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<?php
$query1=mysql_query("select * from lab_location_package_rel where city_id='$selectcity1' AND area_id='$selectarea1' AND lab_id='$selectlab1'") or die (mysql_error());
while($value = mysql_fetch_array($query1)) {
$mrp=$value['mrp'];
$opp=$value['offer_price_perc'];
$package_id=$value['package_id'];
$per=$mrp*$opp/100;
$total=$mrp-$per;
$query2=mysql_query("select * from package_master where id='$package_id'") or die (mysql_error());
$value2 = mysql_fetch_array($query2);
?>
<li class="list-group-item">
<div class="checkbox">
<input type="checkbox" id="checkbox" name="namec[]" value="<?php echo $value["id"]; ?>" />
<label for="checkbox">
<b>Package Name:</b> <?php $package_title1=$value2['package_title']; echo $package_title1;?> <br>
<b>MRP:</b> <strike><?php $mrp1=$value['mrp']; echo $mrp1; ?> </strike><br>
<b>Offer Price:</b> <?php $total1=$total; echo $total1; ?>
</label>
</div>
</li>
<?php }
?>
<input type="submit" class="pull-right btn btn-warning" value="Submit" name="submit">
</form>
Now After submission, for display code:
<div class="col-md-5">
<h5>Patient Details</h5>
<table class="table table-bordered">
<tbody>
<tr>
<th>Name</th>
<th>Age</th>
<th>Sex</th>
<th></th>
</tr>
<tr>
<?php if(isset($_POST["submit"])==true)
{ ?>
<td><?php echo $_POST['name1']; ?></td>
<td><?php echo $_POST['age']; ?></td>
<td><?php echo $_POST['selectgender']; ?></td>
<td>Update</td>
<?php } ?>
</tr>
</tbody>
</table>
<br /><br />
<h5>Package Details</h5>
<table class="table table-bordered">
<tbody>
<tr>
<th>Package</th>
<th>MRP</th>
<th>Offer Price</th>
<th></th>
</tr>
<tr>
<?php
extract($_POST);
foreach($_POST['namec'] as $selected){
$query1=mysql_query("select * from lab_location_package_rel where id='$selected'") or die (mysql_error());
while($value = mysql_fetch_array($query1)) {
$mrp=$value['mrp'];
$opp=$value['offer_price_perc'];
$package_id=$value['package_id'];
$per=$mrp*$opp/100;
$total=$mrp-$per;
$query2=mysql_query("select * from package_master where id='$package_id'") or die (mysql_error());
$value2 = mysql_fetch_array($query2);
?>
<tr>
<td><?php echo $value2['package_title']; ?></td>
<td><strike><?php echo $value['mrp']; ?></strike></td>
<td><?php echo $total; ?></td>
<td>Delete</td>
</tr>
<?php
}
}
}
?>
</tr>
</tbody>
</table>
</div>

Why does my php 'else' displays both at once?

I have this php code which determines if the user has already voted or not.
$row_content['Vote_Who'] contains a list of all users who have previously voted, and is formatted like "User_1 User_2 User_3"
$_SESSION['Username'] is the current username of the user who is signed in. Yes this works, as I have called it elsewhere on the page.
<?php if(strpos($row_content['Vote_Who'], $_SESSION['Username']) !== false){?>
<input type="button" value="Thanks for voting!">
<?php }
else{ ?>
<input type="submit" value="<?php echo $row_content['Votes']?>: Vote Up! ▲" width="75px">
<input type="hidden" name="MM_update" value="vote">
<?php } ?>
But when I see the result in the web browser, both the buttons are displayed at once.
How can I fix that?
Thanks in advance.
UPDATE: Image that proves both buttons display at the same time
UPDATE:
Full code section:
<?php
if ($totalRows_content > 0){
do {?>
<table width="100%">
<tr>
<td width="75px" height="75px" align="right">
<img src="<?php echo $row_content['User_Pic']?>" width="50px" height="50px">
<br />
<?php echo $row_content['Author']?>
</td>
<td colspan="2">
<h2>
<?php echo $row_content['Title']?>
</h2>
</td>
</tr>
<tr>
<td>
</td>
<td style="vertical-align:top">
<h3>
<?php echo $row_content['Content']?>
<br />
<br />
</h3>
</td>
<td width="150px" style="vertical-align:top">
<img src="<?php echo $row_content['Picture']?>" style="max-width:150px; max-height:100px">
<h3>
<?php echo $row_content['Published']?>
</h3>
<br />
<?php if(isset ($_SESSION['Username'])){?>
<form method="POST" action="<?php echo $editFormAction; ?>" name="vote">
<input type="text" hidden="hidden" name="ID" value="<?php echo $row_content['ID']?>">
<input type="text" hidden="hidden" name="votes_up" value="<?php echo ($row_content['Votes'] + 1)?>">
<?php if(strpos($row_content['Vote_Who'], $_SESSION['Username']) !== false){?>
<input type="button" value="Thanks for voting!">
<?php }
else{ ?>
<input type="submit" value="<?php echo $row_content['Votes']?>: Vote Up! ▲" width="75px">
<input type="hidden" name="MM_update" value="vote">
<?php } ?>
</form>
<a href="comments.php?ID=<?php echo $row_content['ID']?>">
<input type="button" value="See Comments">
</a>
<?php }
else{?>
<?php echo $row_content['Votes']?> Votes - You must be signed in to vote
<?php }?>
</td>
</tr>
</table>
<?php }while ($row_content = mysql_fetch_assoc($content));}
else{?>
there is nothing
<?php }
?>
The problem was where I'd used the operator of
!==
when what I should have used was
!=
Example:
strpos($row_content['Vote_Who'], $_SESSION['Username']) != false
Thanks to everyone who offered assistance with this issue.

PHP: retrieve value from within if statement to use within html input tags on same page

i am trying to retrieve values from an if statement, that is getting the values from a database here is the code i have;
<!DOCTYPE html>
<?PHP
include('session.php');
include('recipedbconfig.php');
if(isset($_POST['submit'])) {
$postrcpnmesrch = $_POST['rcpnmesrch'];
if($_SERVER["REQUEST_METHOD"] == "POST") {
$sql = "SELECT * FROM recipe_table WHERE recipe_name = '".$postrcpnmesrch."'";
$result = mysqli_query($db, $sql);
$row = mysqli_fetch_assoc($result);
}
}
?>
<html>
<head>
<title>Fresh Veg Admin, Edit Recipes</title>
<link rel=stylesheet type="text/css" href="/FVRC/FVRC.css">
</head>
<body>
<h1>Fresh Veg Admin, Edit Recipes</h1>
<p>User:<b> <?PHP echo $login_session; ?></b>, logged in.</p>
<form action="/FVRC/addeditrecipes.php">
<button type="submit">Back</button>
</form>
<br/>
<hr/>
<table>
<form method="POST">
<tr>
<td>
<input type="text" placeholder="Search for recipes" name="rcpnmesrch">
</td>
<td>
<button type="submit">Search</button>
</td>
</tr>
</form>
</table>
<br/>
<hr/>
<form action="editrecipecheck.php" method="POST">
<table cellspacing=0 cellpadding=5>
<tr>
<td>
</td>
<td>
<label>Recipe Name:</label>
</td>
<td>
<input type="text" placeholder="Recipe Name" name="rcpnme" value="<?php echo $row['recipe_name']; ?>" required>
</td>
</tr>
<tr>
<td>
<label>Ingredient One:</label>
</td>
<td>
<input type="text" placeholder="Ingredient" name="ingnme1" required>
</td>
i need the values that the $row array contains, so it can be referenced to in the inputs:
<td>
<input type="text" placeholder="Recipe Name" name="rcpnme" value="<?php echo $row['recipe_name']; ?>" required>
</td>
but as it is local i cant figure it out, i may just be having a brain fart moment, any help would be appreciated
all i get as an error message is this:
<br />
<b>Notice</b>: Undefined index: recipe_name in <b>E:\Program Files\xampp\htdocs\FVRC\editrecipe.php</b> on line <b>52</b>
<br />

dynamic checkbox value access in another file in PHP

i am facing problem i am developing school system in php.
i want use checkbox dynamic for attendance but checkbox value i cant use in another file please help me
first file attendacne.php
<form name="abc" method="post" action="attendance-con.php">
<p>Date:
<input name="dat" type="text" class="tcal"/>
<input name="Save" type="submit" style="float:right 20ox;" value="Save Attendace" />
<input name="class_id" type="hidden" value="<?php echo $_REQUEST['class_id']; ?> " />
<?php
$a=0;
$qry=("SELECT DISTINCT
s.rollnum as rollnum,
s.std_name as name
FROM student s WHERE s.std_class=".$_REQUEST['class_id']."");
$result=mysql_query($qry);
while($row=mysql_fetch_array($result))
{
?>
<table width="100%" border="1" align="center" cellpadding="4" cellspacing="4" style="text-align:left;" id="stname">
<?php
if($a==0)
{ ?>
<tr>
<th width="117" height="39" bgcolor="#FFE1CC">Student RollNo</th>
<th bgcolor="#FFE1CC">Student Name</th>
<th width="100" bgcolor="#ECFAFF">Present</th>
</tr>
<?php
}?>
<?php
if($a==1)
{ ?>
<tr>
<th width="117" height="-5" bgcolor="#FFE1CC"></th>
<th bgcolor="#FFE1CC"></th>
<th width="100" bgcolor="#ECFAFF"></th>
</tr>
<?php
}
?>
<?php $a=1; ?>
<input type="hidden" name="rollnum[]" value="<?php echo $row['rollnum'];?>"/><tr>
<td>
<?php echo $row['rollnum'];?>
</td>
<td>
<?php echo $row['name'];?>
</td>
<td>
<input type="checkbox" name="stid[]" value="1">
</td>
</tr>
<?php }?>
</table>
</form>
second file attendance-con.php
<?php
include_once("include/config.php");
$i=0;
$Dat=$_POST['dat'];
$cid=$_POST['class_id'];
$sid=$_POST['stid'];
foreach($_POST['rollnum'] as $x)
{
if($sid[$i]=='1')
{
$query=mysql_query("INSERT INTO attendance (sid,cid,Date,status)
values('$x','$cid','$Dat','1')");
}
if($sid[$i]!='1')
{
$query=mysql_query("INSERT INTO attendance (sid,cid,Date,status)
values('$x','$cid','$Dat','0')");
}
$i=$i+1;
}
?>
<input type="checkbox" name="stid" value="1">
not
<input type="checkbox" name="stid[]" value="1">

Categories