I am a PHP programmer, I am confused with the problem below, I am waiting for your guide .
Thanks so much!
There is the html code
<form action="" method="POST">
<div>
<strong>Release: *</strong> <input type="text" name="Release" value="<?php echo $rel; ?>" /><br/>
<strong>User Story ID: *</strong> <input type="text" name="User Story ID" value="<?php echo $id; ?>" /><br/>
<strong>Test Owner *</strong> <input type="text" name="Test Owner" value="<?php echo $owner; ?>" /><br/>
<strong>Date of TC Review *</strong> <input type="text" name="Date of TC Review" value="<?php echo $data; ?>" /><br/>
<strong>By Design </strong> <input type="text" name="By Design" value="<?php echo $design; ?>" /><br/>
<strong>By Review </strong> <input type="text" name="By Review" value="<?php echo $review; ?>" /><br/>
<strong>By Defect </strong> <input type="text" name="By Defect" value="<?php echo $defect; ?>" /><br/>
<p>* required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
There is the php code
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$release = mysql_real_escape_string(htmlspecialchars($_POST['Release']));
// $ID = " abc";
echo $_POST['Release'];
echo $_POST['User Story ID'];
$ID = mysql_real_escape_string(htmlspecialchars($_POST["User Story ID"]));
$T_Owner = mysql_real_escape_string(htmlspecialchars($_POST['Test Owner']));
$data = mysql_real_escape_string(htmlspecialchars($_POST['Date of TC Review']));
$T_ByDesign= mysql_real_escape_string(htmlspecialchars($_POST['By Design']));
$T_ByReview= mysql_real_escape_string(htmlspecialchars($_POST['By Review']));
$T_ByDefect= mysql_real_escape_string(htmlspecialchars($_POST['By Defect']));
// check to make sure both fields are entered
if ($release == '' || $ID == ''||$T_Owner==''||$data=='')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($release, $ID, $T_Owner, $data, $T_ByDesign, $T_ByReview, $T_ByDefect, $error);
}
else
{
// save the data to the database
mysql_query("INSERT Tests SET T_Release='$release', ID='$ID',TestOwner='$T_Owner',T_Date='$data',Test_ByDesign='$T_ByDesign',Test_ByReview='$T_ByReview',Test_ByDefect='$T_ByDefect'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
}
}
And the error information is below:
Notice: Undefined index: User Story ID in C:\xampp\htdocs\Test\new.php on line 47
abc
Notice: Undefined index: User Story ID in C:\xampp\htdocs\Test\new.php on line 55
Notice: Undefined index: User Story ID in C:\xampp\htdocs\Test\new.php on line 56
Notice: Undefined index: Test Owner in C:\xampp\htdocs\Test\new.php on line 57
Notice: Undefined index: Date of TC Review in C:\xampp\htdocs\Test\new.php on line 58
Notice: Undefined index: By Design in C:\xampp\htdocs\Test\new.php on line 59
Notice: Undefined index: By Review in C:\xampp\htdocs\Test\new.php on line 60
Notice: Undefined index: By Defect in C:\xampp\htdocs\Test\new.php on line 61
You are using invalid name for form input element.
Don't include space in form name.
1st edit your form:
<form action="" method="POST">
<div>
<strong>Release: *</strong> <input type="text" name="Release" value="<?php echo $rel; ?>" /><br/>
<strong>User Story ID: *</strong> <input type="text" name="User_Story_ID" value="<?php echo $id; ?>" /><br/>
<strong>Test Owner *</strong> <input type="text" name="Test_Owner" value="<?php echo $owner; ?>" /><br/>
<strong>Date of TC Review *</strong> <input type="text" name="Date_of_TC_Review" value="<?php echo $data; ?>" /><br/>
<strong>By Design </strong> <input type="text" name="By_Design" value="<?php echo $design; ?>" /><br/>
<strong>By Review </strong> <input type="text" name="By_Review" value="<?php echo $review; ?>" /><br/>
<strong>By Defect </strong> <input type="text" name="By_Defect" value="<?php echo $defect; ?>" /><br/>
<p>* required</p>
<input type="submit" name="submit" value="Submit">
</div>
don't use spaces on the form names, then you can get the post values like this:
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$release = mysql_real_escape_string(htmlspecialchars($_POST['Release']));
echo Print_r($_POST['Release']);
echo Print_r($_POST['User_Story_ID']);
$ID = $_POST["User_Story_ID"];
$T_Owner = $_POST['Test_Owner'];
$data = $_POST['Date_of_TC_Review'];
$T_ByDesign= $_POST['By_Design'];
$T_ByReview= $_POST['By_Review'];
$T_ByDefect= $_POST['By_Defect'];
// check to make sure both fields are entered
if ($release == '' || $ID == ''||$T_Owner==''||$data=='')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($release, $ID, $T_Owner, $data, $T_ByDesign, $T_ByReview, $T_ByDefect, $error);
}
else
{
// save the data to the database
mysql_query("INSERT Tests SET T_Release='$release', ID='$ID',TestOwner='$T_Owner',T_Date='$data',Test_ByDesign='$T_ByDesign',Test_ByReview='$T_ByReview',Test_ByDefect='$T_ByDefect'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
}
First of all never use "By Design" kinds of coding in php or in any other programming instead of it you should use like this "By_Design" or use cameCaps(byDesign). What is $rel,$id and other stuffs in your html form, you didnot mentioned it. The main problem that is causing in your problem is the use of spaces in your forms and in php.Please don't use it.
I tried some test code:
<form action="" method="POST">
<input type="submit" name="what happened" value="here" />
</form>
<?php
print_r($_POST);
?>
And it seems PHP replaces all elements with spaces in their names to underscores. Thus you'd want to use $_POST['User_Story_ID'] instead of $_POST['User Story ID']. Etc.
You should be define you variable before use. Please check one more time.
Add form action "your_page.php"
Example : <form action="your_page.php" method="POST">
And remove space for form input element like:
echo $_POST['User Story ID'];
instead of this :
Example :
<strong>User Story ID: *</strong> <input type="text" name="UserStoryID" value="<?php echo $id; ?>" /><br/>
echo $_POST['UserStoryID']; or whatever you want to name it
Related
I created a section for editing. When I edit the information and click the save button, the information is not saved and the header section does not display completely.
<?php
if (isset($_POST['submit_btn']))
{
$id = $_POST['id'];
$fn = trim($_POST['name']);
$ln = trim($_POST['lastname']);
$age = trim($_POST['age']);
$q = "UPDATE `users` SET `fn` = '$fn',
`ln` = '$ln',
`age` = '$age'
WHERE id = '$id'";
mysqli_query($dbconnect,$q);
if (mysqli_affected_rows($dbconnect) > 0)
redirect("?msg=ok&id=**$id**");
else
redirect("?msg=error&id=**$id**");
}
else
echo ("Not In If(isset)");
?>
<form action="" method="post">
<label for="name">FirstName:</label>
<input type="text" name="name" id="name" value="<?php echo $row['fn']?>">
<br>
<label for="lastname">LastName:</label>
<input type="text" name="lastname" id="lastname" value="<?php echo $row['ln']?>">
<br>
<label for="age">Age:</label>
<input type="text" name="age" id="age" value="<?php echo $row['age']?>">
<br>
<input type="submit" name="submit_btn" value="Save">
<a href="index2.php">
Back
</a>
</form>
</body>
Bold sections do not work here.
Below is a picture of the result:
In the link that I specified, after clicking on save the ID will not be displayed and all the information filled in the forms will be lost.
Sorry if the result is styleless and boring and I just created this page to practice php😁
Thank you for being responsive🙏🙏🙏
You are mistaking a POST request with a GET request.
Part, which appears in the URL is sent to the webserver in GET request.
Your form is submitting POST request to the webserver, logic in the code does the same, but you are trying to display information from url (GET).
Please check the examples in php.net:
POST variables: https://www.php.net/manual/en/reserved.variables.post.php
GET variables: https://www.php.net/manual/en/reserved.variables.get.php
You can take an example with GET request variable below, however, be careful with trusting the "end client" and always prepare your statements, which you send to your database to execute queries.
if (isset($_GET['submit']))
{
$number = $_GET['number'];
echo $number
? "Number which was submitted: $number <br>"
: 'Number is not set';
} else {
echo 'Form has not been yet submitted';
}
?>
<form action="" method="get">
<input type="number" name="number" placeholder="Number">
<input type="submit" name="submit" value="Save">
</form>
I dont know what im doing wrong. But i get this
Notice: Undefined variable: num1 in D:\Programs\XAMPP\htdocs\homework\addsub.php on line 15`
<?php
if(isset($_POST['sub']))
{
$num1=$_POST['t1'];
$num2=$_POST['t2'];
if ($_POST['sub']=="+") {
$res= $num1 + $num2;
}
elseif($_POST['sub']=="-"){
$res = $num1-$num2;
}
}
?>
<form action="addsub.php" method="POST">
<input type="text" name="t1" value="<?php echo $num1;?>"><br>
<input type="text" name="t2" value="<?php echo $num2;?>"><br>
<input type="text" name="res" value="<?php echo $res;?>"><br>
<input type="submit" name="sub" value="+">
<input type="submit" name="sub" value="-">
</form>
When I use $num1 or $num2 in textbox values, it shows error. One of my friends used this same code on his laptop but he is using much older version of Xampp. It works fine but later versions of Xampp gives this error. I am using Xampp v3.2.1.
Or initialize the variable if it doesn't exist, like so:
<?php
if (!isset($num1)) {
$num1 = '';
}
Then your HTML could remain unchanged.
The reason I recommend this approach is that it creates clean code - the HTML will always display the value of $num1 and if you choose to initialize it to a different value later, it should be easier to find in the PHP.
use isset to check variable exist or not.
example
<input type="text" name="t1" value="<?php echo isset($num1)?$num1:""; ?>"><br>
You set the $num1 variable only when,
if(isset($_POST['sub']))
So, if the $_POST['sub'] is not there the variable is undefined!
Here is the code, which may help you:
<?php
var $res="";
var $num1="";
var $num2="";
if(isset($_POST['sub']))
{
$num1=$_POST['t1'];
$num2=$_POST['t2'];
if ($_POST['sub']=="+") {
$res= $num1 + $num2;
}
elseif($_POST['sub']=="-"){
$res = $num1-$num2;
}
}
?>
<form action="addsub.php" method="POST">
<input type="text" name="t1" value="<?php echo $num1;?>"><br>
<input type="text" name="t2" value="<?php echo $num2;?>"><br>
<input type="text" name="res" value="<?php echo $res;?>"><br>
<input type="submit" name="sub" value="+">
<input type="submit" name="sub" value="-">
</form>
I've been tearing my hair out trying to figure out why the isset($_POST['Submit']) is not executing with my form. The data from the form is just not passing into the php code. Basically the code does not seem to be recognizing something like $ffname = $_POST["ffname"];
<?php
$ffname = $flname = $femail = $fcemail = $fpass = $fcpass = "";
if(isset($_POST['ffname'])){
$ffname = $_POST["ffname"];
$flname = $_POST["flname"];
$femail = $_POST["femail"];
$fcemail = $_POST["fcemail"];
$fpass = $_POST["fpass"];
$fcpass = $_POST["fcpass"];
echo "<p>Hello World<p>";
$con = mysqli_connect("localhost", "root", "") or die(mysqli_error());
mysqli_select_db($con, "userdata") or die(mysqli_error($con));
mysqli_query($con,"INSERT INTO tbluser (fname, lname, email, pass) VALUES('$ffname', '$flname', '$femail', '$fpass')")
or die (mysqli_error($con));
}
?>
<form method="post">
First Name: <input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Last Name: <input type="text" name="flname" value="<?php echo $flname;?>"><br>
E-mail: <input type="email" name="femail" value="<?php echo $femail;?>"><br>
Confirm E-mail: <input type="email" name="fcemail" value="<?php echo $fcemail;?>"><br>
Password: <input type="password" name="fpass" value="<?php echo $fpass;?>"><br>
Confirm Password: <input type="password" name="fcpass" value="<?php echo $fcpass;?>"><br>
<input type="submit" name="Submit" value="submit">
</form>
The other answer by #DerVO is correct. But there seems to be something else at play, since you say it still doesn't work.
A comment became too long, so I've built a full answer here.
Step 1:
Add a name to your input:
<input type="submit" name="Submit" value="submit">
However, relying on the submit in your $_POST is not the best plan. So I suggest watching a different form field - for example, ffname:
Step 2:
Improve your watch, using a different field:
if ( isset( $_POST['ffname'] ) ) {
// do your work
}
Lastly, you may be munging your form action attribute.
Step 3:
In order to keep things simple, if the form is supposed to submit to the same page, you can simply omit the form action.
<form method="post">
Betweeen these three items, the form will work, unless you have some problem with your server.
Step 4:
Clean up your form formatting. You've got odd spacing which is problematic. In an html element, the property="value" code needs to be without spaces, but spaces between properties. Example:
<!-- Your version -->
<input type = "text"name = "ffname"id = "ffname"value="<?php echo $ffname;?>"><br>
<!-- Clean / correct version -->
<input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Here's a "clean" version of your whole form:
<form method="post">
First Name: <input type="text" name="ffname" id="ffname" value="<?php echo $ffname;?>"><br>
Last Name: <input type="text" name="flname" value="<?php echo $flname;?>"><br>
E-mail: <input type="email" name="femail" value="<?php echo $femail;?>"><br>
Confirm E-mail: <input type="email" name="fcemail" value="<?php echo $fcemail;?>"><br>
Password: <input type="password" name="fpass" value="<?php echo $fpass;?>"><br>
Confirm Password: <input type="password" name="fcpass" value="<?php echo $fcpass;?>"><br>
<input type="submit" name="Submit" value="submit">
</form>
You need to give your input submit a name:
<input type="submit" name="Submit" value="Submit">
You have pass name of element in $_POST
try put name attribute in input submit
<input type = "submit" name="Submit" value = "1">
I need to insert and extract data from a MySQL database. I'm able to extract info, but when I try to insert it, it gives me a lot of error messages. Most of them I was able to resolve, but this one I just can't seem to figure out:
Notice: Undefined variable: emailInput in C:\xampp\htdocs\imp02\week5\firstPHPDatabase.php on line 25
Notice: Undefined variable: aanmelding in C:\xampp\htdocs\imp02\week5\firstPHPDatabase.php on line 25
Notice: Undefined variable: ipadres in C:\xampp\htdocs\imp02\week5\firstPHPDatabase.php on line 25
Notice: Undefined variable: opmerkingen in C:\xampp\htdocs\imp02\week5\firstPHPDatabase.php on line 25
Notice: Undefined variable: aantalPersonen in C:\xampp\htdocs\imp02\week5\firstPHPDatabase.php on line 25
My code:
<?php
$databaseLink = mysqli_connect('localhost', 'root', '', 'newyearseveparty');
if (mysqli_connect_error())
echo mysqli_connect_error();
$selectorQuery = "SELECT * FROM attendants";
echo "The query We use is $selectorQuery!";
$attendants = array();
if ($result = mysqli_query($databaseLink, $selectorQuery)) {
while ($tableRow = mysqli_fetch_assoc($result)) {
$attendants[] = $tableRow;
}
} else {
echo mysqli_error($databaseLink) . 'QUERY: ' . $$selectorQuery;
}
if (isset($_POST['value'])){
$nameInput = $_POST['nameInput'];
$emailInput = $_POST['emailInput'];
$aanmelding = $_POST['aanmelding'];
$ipadres = $_SERVER['REMOTE_ADDR'];
$opmerkingen = $_POST['opmerkingen'];
$aantalPersonen = $_POST['aantalpersonen'];
}
$sql = "INSERT INTO attendants (naam, email, komt, ipadres, opmerkingen, aantalpersonen)
VALUES('Justin', '$emailInput', '$aanmelding', '$ipadres','$opmerkingen', '$aantalPersonen')";
if (!mysqli_query($databaseLink,$sql))
{
die('Error: ' . mysqli_error($databaseLink));
}
echo "1 record added";
mysqli_close($databaseLink);
?>
<!doctype html>
<html>
<head>
<title></title>
<meta name="description" content=""/>
<meta charset="utf-8"/>
<link rel="stylesheet" href=""/>
</head>
<body>
<?php
if (!empty($attendants)) {
foreach ($attendants as $people) {
echo '<ol>';
echo 'Attendent';
echo "<li>Naam: {$people['naam']}</li>";
echo "<li>E-mail: {$people['email']}</li>";
echo "<li>Komt: {$people['komt']}</li>";
echo "<li>Datum van aanmelding: {$people['datum']}</li>";
echo "<li>Ipadres: {$people['ipadres']}</li>";
echo "<li>Eventuele opmerkingen: {$people['opmerkingen']}</li>";
echo "<li>Aantal personen: {$people['aantalpersonen']}</li>";
echo '</ol>';
}
} else {
echo "af er is iets fout gegaan, of er heeft nog niemand zich ingeschreven";
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<fieldset>
<label for="nameInput" class="labelstyle">Naam: </label>
<input id="nameInput" name="inputFields" type="text" autofocus="auto"><br>
<label for="emailInput" class="labelstyle">E-Mail</label>
<input id="emailInput" name="inputFields" type="text"><br>
<label for="aanmelding" class="labelstyle">Komt u, voor ja 1, voor nee 0</label>
<input id="aanmelding" name="inputFields" type="text"><br>
<label for="opmerkingen" class="labelstyle">opmerkingen</label>
<input id="opmerkingen" name="inputFields" type="text"><br>
<label for="aantalpersonen" class="labelstyle">aantal personen</label>
<input id="aantalpersonen" name="inputFields" type="number"><br>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
</body>
</html>
I hope some of you would be so nice to help me out
The name of the input element will be posted as the key of $_POST array.So change it like this or change your $_POST keys.
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<fieldset>
<label for="nameInput" class="labelstyle">Naam: </label>
<input id="nameInput" name="nameInput" type="text" autofocus="auto"><br>
<label for="emailInput" class="labelstyle">E-Mail</label>
<input id="emailInput" name="emailInput" type="text"><br>
<label for="aanmelding" class="labelstyle">Komt u, voor ja 1, voor nee 0</label>
<input id="aanmelding" name="aanmelding" type="text"><br>
<label for="opmerkingen" class="labelstyle">opmerkingen</label>
<input id="opmerkingen" name="opmerkingen" type="text"><br>
<label for="aantalpersonen" class="labelstyle">aantal personen</label>
<input id="aantalpersonen" name="aantalpersonen" type="number"><br>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
You cant access a POST element using the id of the element.For accessing an element as $_POST['nameInput'] the NAME of the input field should be nameInput
eg:-
<input id="nameInput" name="inputFields" type="text" autofocus="auto"><br>
should be changed to
<input id="nameInput" name="nameInput" type="text" autofocus="auto"><br>
AND
If you want the form to be submitted to the same page. you should leave the action attribute of the form blank instead like action=''
AND
The name of your submit button is 'submit'. so you should check like this if the form has been submitted or not isset($_POST['submit'])
Use isset($_POST['value']) to handle the situation
use like this: "$_POST[variableName]";
Dont't give single quotes to the variable inside post.
You cant access a POST element using the id of the element.For accessing an element as $_POST['nameInput'] the name of the input field should be nameInput
In example:
<input id="nameInput" name="inputFields" type="text" autofocus="auto"><br>
should be changed to
<input id="nameInput" name="nameInput" type="text" autofocus="auto"><br>
If you want the form to be submitted to the same page. you should leave the action attribute of the form blank instead. (like action='')
The name of your submit button is 'submit'. so you should check like this if the form has been submitted or not isset($_POST['submit'])
I've created a form for the user to fill out and on submit it is processed and stuff is done with it. Usually just a database query.
With the database that I am working with, some fields can be NULL, so the user could leave something blank in some of the fields. However, when testing this, I tried having one or more fields empty, but the form wouldn't really submit anything at all. When I was debugging, almost all the values of the fields, even the ones with text in it, turn out NULL when I retrieve the values from the POST method.
The thing is, I need to be able to allow the user to submit a form with some fields blank, as they are not completely necessary. How would I go about this?
Do I have to check isset() for everything that could be blank and set it to NULL if it is?
Thanks in advance!
EDIT: Here is the form code.
<form onsubmit="return confirm('Are you sure you want to save the edits?');" method="post" action="orgedit.php?id=<?php echo $organization_id?>" id="orgform" name="orgform" >
Parent Organization ID: <input type="text" name="parent_id" value="<?php echo $row['parent_id']; ?>"/> <br /><br />
Organization nm: <input type="text" name="org_nm" value="<?php echo $row['org_nm']; ?>"/> <br /><br />
TBR Organization Sysnm: <input type="text" name="tbr_sysnm" value="<?php echo $row['tbr_sysnm']; ?>"/> <br /><br />
Type: <input type="text" name="type" value="<?php echo $row['type']; ?>"/> <br /><br />
Contact nm: <input type="text" name="contact_nm" value="<?php echo $row['contact_nm']; ?>"/> <br /><br />
Financial Info: <input type="text" name="financial_info" value="<?php echo $row['financial_info']; ?>"/> <br /><br />
Other Info: <input type="text" name="other_info" value="<?php echo $row['other_info']; ?>"/> <br /><br />
Active: <input type="text" name="active" value="<?php echo $row['active']; ?>"/> <br /><br />
URL: <input type="text" name="url" value="<?php echo $row['url']; ?>"/> <br /><br />
<input type="submit" value="Save Entry" name="save" />
</form>
and here is the php processing code :)
if(isset($_GET['id']))
{
$organization_id = $_GET['id'];
$parent_organization_id = $_POST['parent_organization_id'];
$organization_nm = $_POST['organization_nm'];
$tbr_organization_sysnm = $_POST['tbr_organization_sysnm'];
$type = $_POST['type'];
$contact_nm = $_POST['contact_nm'];
$financial_info = $_POST['financial_info'];
$other_info = $_POST['other_info'];
$active = $_POST['active'];
$url = $_POST['url'];
After I get the values I simply escape them and perform a query.
I have figured out the problem!
if(isset($_GET['id']))
{
$organization_id = $_GET['id'];
$parent_organization_id = $_POST['parent_id'];
$organization_nm = $_POST['org_nm'];
$tbr_organization_sysnm = $_POST['tbr_sysnm'];
$type = $_POST['type'];
$contact_nm = $_POST['contact_nm'];
$financial_info = $_POST['financial_info'];
$other_info = $_POST['other_info'];
$active = $_POST['active'];
$url = $_POST['url'];
I was retrieving the wrong values from the form. This could've been fixed by either changing the php code or the html code.
The problem is, the one or more fields empty issue still persists. If one of the fields is blank, the entry is not saved. I've checked the logs and the message it outputted was:
PHP Notice: Undefined index: parent_id
PHP Notice: Undefined index: org_nm
PHP Notice: Undefined index: tbr_sysnm
This happens when I don't write anything for any of those fields and try to save the form.
EDIT 2: The problem was fixed once again. Did a var dump and found that the server wasn't giving out anything when the code was trying to retrieve the values. It was a spelling mistake in a few of the fields.
My first guess would be:
Probably your form inputs don't have a name attribute. Input data is only submitted if the input has a name attribute and the PHP $_POST key will be the name of the attribute.
Let's see OP's code to see if my guess is correct.