Form submission on php custom templated system - php

We are using very simple php custom template system using oop approach, but when we submit form, its not getting $_POST[] results. Please have a look at main code...
switch($act) { //$act= $_GET['Act'];
case 'Add':
$add_product = new Product();
print_r($_POST);
if(!empty($_POST)) {
echo $_POST['name']; //to check if its getting value
}
include('templates/edit_product.tpl');
break;
}
and here is edit_product.tpl...
<form action="<?php echo BASE_URL; ?>products.php?Act=Add" method="post">
<input type="text" size="50" name="name" />
<button type="submit" name="submit">submit</button>
thanks for your support.

<form action="<?php echo BASE_URL; ?>products.php?Act=Add" method="post">
<input type="text" size="50" name="name" />
<input type="submit" name="submit" value="submit" />
</form>

we got the problem, Thanks. We were using if(!empty($errors)) instead of if(empty($errors))

Related

POST not working with no signs of error

haven't programmed PHP in a while but I
have to assemble something for a client really fast.
I've set up 2 forms with POST but when I go to the next file it's just blank space, for some reason POST isn't being registered but is set cause I'm not getting an error echo.
Hese's the forms:
<form action="Funkcije.php" method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj">
</form>
<br>
<div id="newItem">
<form action="Funkcije.php" method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj">
</form>
</div>
And here's the 2nd file:
if(isset($_POST["AddFromDB"], $_POST["ArtNo"])){
addExisting ($_POST["ArtNo"]);
}
else if(isset($_POST["AddNew"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
newItem ($_POST["Art"] && $_POST["ImeProizvoda"] && $_POST["Dobava"] && $_POST["Cijena"]);
}
else if (!isset ($_POST)){
echo "error";
}
So, by code I should be getting an error if POST is not set but I get nothing. Just a blank space.
here, you must be give a name to the submit button to check which form is POST like this...
<form method="post" name="AddFromDB">
<input type="text" placeholder="Šifra Art" name="ArtNo">
<input type="submit" value="Dodaj" name="form1">
</form>
<br>
<div id="newItem">
<form method="post" name="AddNew">
<input type="text" placeholder="Šifra" name="Art">
<input type="text" placeholder="Ime Proizvoda" name="ImeProizvoda">
<input type="text" placeholder="Dobavljač" name="Dobava">
<input type="text" placeholder="Cijena" name="Cijena">
<input type="submit" value="Dodaj" name="form2">
</form>
</div>
<?php
if(isset($_POST["form1"], $_POST["ArtNo"])){
echo "1";
}
else if(isset($_POST["form2"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
echo "2";
}
else{
echo "error";
}
?>
now, this work fine..
thank you.. enjoy coding...

php POST array not set

i am trying to collect data from the user in a form and display the data back to him .
i am using WAMP.
here is my html code
<FORM METHOD="POST" ACTION="submit.php">
<INPUT type="text" name="URL" size=17 value="http://">
<INPUT type="text" name="user" size=17>
<INPUT type="text" name="email" size=17>
<INPUT type="submit" value="Submit" name="submit"/>
<INPUT type=reset value="Clear">
</form>
here is my submit.php code
<?php
if (isset($_POST['URL'])){
echo "set";
}
else
{
echo "not set";
}
?>
when i execute this i am always getting "not set" as the output.
thanks.
It should be $_POST not $post, so your code should be :-
<?php
if (isset($_POST['URL'])){
echo "set";
}
else
{
echo "not set";
}
?>
Also your form tag should not contain encytype="text/plain" because PHP doesn't handle it (and it is not a bug)
Valid values for enctype in <form> tag are:
application/x-www-form-urlencoded
multipart/form-data
So remove encytype="text/plain"
<?php
if (isset($_POST))
{
echo "set";
}
else
{
echo "not set";
}
?>
try this
Remove
ENCTYPE="text/plain"
from the form
So the form should look like
<form method="POST" action="submit.php">
And its $_POST not $POST
if (isset($POST['URL'])){
should be
if (isset($_POST['URL'])){
Here is an example of handling the form
<?php
if(isset($_POST["submit"])){
if (isset($_POST['URL']) && $_POST['URL'] != ''){
echo "set";
}
else
{
echo "not set";
}
}
?>
<FORM METHOD="POST" ACTION="submit.php" >
<INPUT type="text" name="URL" size=17 value="http://">
<INPUT type="text" name="user" size=17>
<INPUT type="text" name="email" size=17>
<INPUT type="submit" value="Submit" name="submit"/>
<INPUT type=reset value="Clear">
</form>

access $_GET value after form submission

example.php gets value of x by $_GET['x'] from another page.
I have a form in the same page (example.php). Upon submission of the form, I am loosing the value of x.
My question is: How can I keep and access the value of x after submission of the form.
Code is like this:
<body>
<?php
if(isset($_GET['y'])) {
$y = $_GET['y']; // I have also tried with $_REQUEST
}
if($_POST['submit']) {
$x = $_POST['field1'];
echo $y."<br>";
echo $x;
}
?>
<form name="form1" action="example.php" method="post">
<input type="text" name="field1" />
<input type="submit" name="submit" value="Echo the name" />
</form>
</body
I have also tried the following as per suggestion, but its not working:
<body>
<?php
if(isset($_POST['submit'])) {
$x = $_POST['field1'];
$y = $_POST['y'];
echo $y."<br>";
echo $x;
}
?>
<form name="form1" action="dollar_get_and_form1.php" method="post">
<input type="text" name="field1" />
<input type="hidden" name="y" value="<?php htmlentities($_GET['y']) ?>" />
<input type="submit" name="submit" value="Echo the name" />
</form>
</body>
Issue is solved by the following codes:
<body>
<?php
if(isset($_GET['x'])) {
$x = $_GET['x'];
echo $x;
}
if(isset($_POST['submit'])) {
echo $_POST['xValue'].'<br>';
echo $_POST['y'];
}
?>
<form name="form1" action="example.php" method="post">
<input type="text" name="y" />
<input type="submit" name="submit" value="Echo the name" />
<input type="hidden" id="xValue" name="xValue" value="<?php echo $x; ?>"/>
</form>
</body>
Thanks to all for your suggestions. If there are better ways to do this, please suggest so.
Your form is POST not GET so the variable will be gone. The best way to get the desired result is through a hidden form field:
<input type="hidden" name="y" value="<?=htmlentities($_GET['y'])?>" />
Then it will be available as $_POST['y'] when the user submits the form
your form's method is post so, there is not $_GET, and $_GET don't work here...
where is $_GET['y'] comming from?
As HTTP is stateless protocol, Get/Post value persistence is limited to last page and current page. You have to make you use of one of techniques :
"hidden field","session" ,"cookie" or writing/reading to tmp. file to preserve value/state.
use get instead of post method='get'; and create a field(hidden) y like following
<body>
<?php
if(isset($_GET['y'])) {
$y = $_GET['y']; // I have also tried with $_REQUEST
}
if($_GET['submit']) {
$x = $_GET['field1'];
echo $y."<br>";
echo $x;
}
?>
<form name="form1" action="example.php" method="get">
<input type="text" name="field1" />
<input type="hidden" name="y" value=1 />
<input type="submit" name="submit" value="Echo the name" />
</form>
</body

Update Statement using Request Method Post

I've done a search related to 'update statement using form'.
Lots of post showed an update function using a form using isset
if(isset($_POST["submit"])) { //process } else { //show form }
Does this mean that it is not possible to do update using this?
if($_SERVER["REQUEST_METHOD"] == "POST") { //process } else { //show form }
It seems so cause my update function doesn't work.
Solved : It works now. I added
<input type="hidden" name="contact_id" value="<?php echo $row["contact_id"]; ?>" />
before
<input type="submit" name="submit" value="Submit" />
in the form
<form method="post" action="update.php">
Username: <input type="text" name="contact_name" value="<?php echo $row["contact_name"]; ?>" />
Email: <input type="text" name="contact_number" value="<?php echo $row["contact_number"]; ?>" />
<input type="hidden" name="contact_id" value="<?php echo $row["contact_id"]; ?>" />
<input type="submit" name="submit" value="Submit" />
</form>
bith ways should work.
But what you should do first is :
var_dump($_POST);
to make sure there is anything in the $_POST var before you do the 1st option.
I don't know if REQUEST_METHOD is always in uppercase. To test this, use strtoupper($_SERVER['REQUEST_METHOD']) == 'POST' and see if it works.

How to access the form's 'name' variable from PHP

I'm trying to create a BMI calculator. This should allow people to use either metric or imperial measurements.
I realise that I could use hidden tags to solve my problem, but this has bugged me before so I thought I'd ask: I can use $_POST['variableName'] to find the submitted variableName field-value; but...I don't know, or see, how to verify which form was used to submit the variables.
My code's below (though I'm not sure it's strictly relevant to the question):
<?php
$bmiSubmitted = $_POST['bmiSubmitted'];
if (isset($bmiSubmitted)) {
$height = $_POST['height'];
$weight = $_POST['weight'];
$bmi = floor($weight/($height*$height));
?>
<ul id="bmi">
<li>Weight (in kilograms) is: <span><?php echo "$weight"; ?></span></li>
<li>Height (in metres) is: <span><?php echo "$height"; ?></span></li>
<li>Body mass index (BMI) is: <span><?php echo "$bmi"; ?></span></li>
</ul>
<?php
}
else {
?>
<div id="formSelector">
<ul>
<li>Metric</li>
<li>Imperial</li>
</ul>
<form name="met" id="metric" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Kilograms">kg</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (<abbr title="metres">m</abbr>):</label>
<input type="text" name="height" id="height" />
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<form name="imp" id="imperial" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Pounds">lbs</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (Inches):</label>
<input type="text" name="height" id="height" /
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<?php
}
?>
I verified that it worked (though without validation at the moment -I didn't want to crowd my question too much) with metric; I've added the form but not the processing for the imperial yet.
To identify the submitted form, you can use:
A hidden input field.
The name or value of the submit button.
The name of the form is not sent to the server as part of the POST data.
You can use code as follows:
<form name="myform" method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="frmname" value=""/>
</form>
You can do it like this:
<input type="text" name="myform[login]">
<input type="password" name="myform[password]">
Check the posted values
if (isset($_POST['myform'])) {
$values = $_POST['myform'];
// $login = $values['login'];
// ...
}
The form name is not submitted. You should just add a hidden field to each form and call it a day.
In the form submitting button (id method of form is post):
<input type="submit" value="save" name="commentData">
In the PHP file:
if (isset($_POST['commentData'])){
// Code
}
For some reason, the name of the submit button is not passed to the superglobal $_POST when submitted with Ajax/jQuery.
Use a unique value on the submit button for each form like so
File index.html
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="contact">Send Message</button>
</form>
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="support">Send Message</button>
</form>
File email.php
<?php
if (isset($_POST["submit"])) {
switch ($_POST["submit"]) {
case "contact":
break;
case "support":
break;
default:
break;
}
}
?>
As petervandijck.com pointed out, this code may be susceptible to XSS attacks if you have it behind some kind of log-in system or have it embedded in other code.
To prevent an XSS attack, where you have written:
<?php echo "$weight"; ?>
You should write instead:
<?php echo htmlentities($weight); ?>
Which could even be better written as:
<?=htmlentities($weight); ?>
You can use GET in the form's action parameter, which I use whenever I make a login/register combined page.
For example: action="loginregister.php?whichform=loginform"
I had a similar problem which brought me to this question. I reviewed all the preceding answers, but ultimately I ending up figuring out my own solution:
<form name="ctc_form" id="ctc_form" action='' method='get'>
<input type="hidden" name="form_nm" id="form_nm">
<button type="submit" name="submit" id="submit" onclick="document.getElementById('form_nm').value=this.closest('form').name;">Submit</button>
</form>
It seamlessly and efficiently accomplishes the following:
Passes the form name attribute via a hidden input field, without using the fallible value attribute of the submit button.
Works with both GET and POST methods.
Requires no additional, independent JavaScript.
You could just give a name to the submit button and do what needs to be done based on that. I have several forms on a page and do just that. Pass the button name and then if button name = button name do something.
Only the names of the form fields are submitted, but the name of the form itself is not. But you can set a hidden field with the name in it.

Categories