Error from posting datas from form using PHP - php

I was wondering could u help me resolve my PHP problem.
I made a PHP script that writes data from form in new row.
But, when i run script it says
Undefined index: name
I am still beginner of PHP but i hope u will help me.
Thanks any way.
<?php
$con=mysqli_connect("localhost","root","","test");
if (mysqli_connect_errno())
{
echo "error". mysqli_connect_error();
}
$name=($_POST['name']);
echo "ime: ".$name;
?>
<form method="post" action="go.php">
FIrst name: <input type="text" name="name"/>
<input type="submit" value="send"?>
</form>

Put it inside this if statement
if (isset($_POST['submit'])) {
$name=($_POST['name']);
echo "ime: ".$name;
}

$_POST['name'] is not defined if you don't submit the form. So, try to type something into the textarea and click on "send": this error won't appear.
Otherwise, you can tell php to consider $_POST['name'] only if it's set, updating your code like this:
<?php
$con=mysqli_connect("localhost","root","","test");
if (mysqli_connect_errno())
{
echo "error". mysqli_connect_error();
}
if(isset($_POST['name'])){$name=($_POST['name']);
echo "ime: ".$name;}
?>
<form method="post" action="go.php">
FIrst name: <input type="text" name="name"/>
<input type="submit" value="send">
</form>

Related

PHP $_POST give empty value

I don't know what happened but there's no value return.
Please check if i made any mistake
Value is blank if you have no check for null or blank value on transfer.php
Case 1 : You are access file in same flow then no need to modify any thing its wokring.If you are access file without press submit button.directly using url then post is blank.
e.g. http://localhost:8080/stackoverflow/transfer.php
Case 2 : MIME type issue, enctype="text/plain" in the form, this should fix the issue.
from_post.php
<html>
<body>
<form action="transfer.php" method="post" enctype="text/plain">
<input type="text" name="name" value="">
<input type="submit" name="submit" value="submit">
</form>
transfer.php
Here you want to check is that submit button if fired or not.
<?php
if(isset($_POST['submit']))
{
$u=$_POST['name'];
if(isset($u) || (!is_empty())){
echo $u;
}
else
{
echo "the post doesn't have any value";
}
echo "<br/>";
}
?>

Displaying the form if the post is empty or result if it has already been submitted

This is my first post and I'm complete beginner so please be gentle :)
I'm trying to create a form that after submitting an account name would check and return a CNAME of the host (account+domain.com)
The problem is that I want to do it all on the same website so it will either display the form if nothing has been posted or display the result otherwise.
This is what I've created, it seems that I'm not calling the POST correctly, but I can't really get what am I doing wrong.
Please help
<?php
if(isset($_POST[DomainSubmit])){
$AccountName = $_POST[ClientDomain];
$CName = dns_get_record($AccountName."domain.com", DNS_CNAME);
echo '<h1>'.$CName.'<h1>';
}
echo'<form action="index.php" method="POST" ">
<input type="text" name="ClientDomain">
<input type="submit" name="DomainSubmit">
</form>'
?>
Try to add else so it will be displayed one or another stuff
<?php
if(isset($_POST['DomainSubmit']) && isset($_POST['ClientDomain'])){
$AccountName = $_POST['ClientDomain'];
$CName = dns_get_record($AccountName."domain.com", DNS_CNAME);
echo '<h1>'.$CName.'<h1>';
} else {
echo'<form action="index.php" method="POST" ">
<input type="text" name="ClientDomain">
<input type="submit" name="DomainSubmit">
</form>'
}
?>
Edit:
You forgot to properly write array POST (missing quotes)
$_POST[DomainSubmit]
And it should be
$_POST['DomainSubmit']

Undefined variable: ... in ...on line 9

I'm trying to make a form that updates a datebase but it gives me two errors. Do you have any idea what it could be from?
The errors:
Notice: Undefined variable: Points inD:\2013.1\xampp\htdocs\ranklist_get.php on line 9
Notice: Undefined variable: Skype in D:\2013.1\xampp\htdocs\ranklist_get.php on line 9
welcome.html
<body>
<form action="ranklist_get.php" method="get">
Skype: <input type="text" id="Skype"><br>
Points: <input type="number" id="Points"><br>
<input type="submit">
</form>
</body>
</html>
ranklist_get.php
<?php
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"UPDATE Persons SET Points='".$Points."' WHERE Skype='".$Skype."'");
mysqli_close($con);
?>
Initialize your variables with the expected values before you use them in your query.
$Points=mysqli_real_escape_string($con,$_GET["Points"]);
$Skype=mysqli_real_escape_string($con,$_GET["Skype"]);
Also make sure to add the name attribute to your form fields. name="Points" and name="Skype", otherwise it wont work.
GET variables are stored in the global $_GET array (just like POST and COOKIE). You can either use them directly in your code like so $_GET["Points"] or store them in a variable.
Please note you should use the name property on each input to specify it's key in the array.
At the top of your code put:
$Points = $_GET["Points"];
$Skype = $_GET["Skype"];
Your form should be rewritten like so:
<form action="ranklist_get.php" method="get">
Skype: <input type="text" id="Skype" name="Skype"><br>
Points: <input type="number" id="Points" name="Points"><br>
<input type="submit">
</form>
You should also sanitize your MySQL query like so:
$query = mysqli->prepare($con, "UPDATE Persons SET Points=? WHERE Skype=?");
$query->bind_param('ss', $points, $skype);
$points = $_GET["Points"];
$skype = $_GET["Skype"];
$query->execute();
You can read more about prepared statements here: http://php.net/manual/en/mysqli.prepare.php
make HTML as
<html>
<body>
<form action="ranklist_get.php" method="get">
Skype: <input type="text" id="Skype" name="Skype"><br>
Points: <input type="number" id="Points" name="Points"><br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
ranklist_get.php
<?php
$con=mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if($_REQUEST['Submit']=='Submit'){
$Points=$_REQUEST['Points'];
$Skype=$_REQUEST['Skype'];
mysqli_query($con,"UPDATE Persons SET Points='".$Points."' WHERE Skype='".$Skype."'");
}
mysqli_close($con);
?>
You have to get the values before you use it.

PHP $_POST doesn't display data - Example code shown

I am new to PHP, I tried to work w3 schools example of posting data on forms..
It never works for me... the webpage doesn't display any data, I tried several forums and also SO that never helped.. I still keep getting it empty!
Example #1: A simple contact from - HTML code
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
Example #2: Printing data from our form
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
Expected output of this script may be:
Hi Joe. You are 22 years old.
Actual Output:
Hi . You are years old
The Post parameter is not displaying data.. Any help is really appreciated.
What W3Schools (PHP Form Handling) fail to mention is, that the entire (2) bodies of code need to either be inside a single file, or in 2 seperate files in order for it to work as expected.
However, the code from W3Schools and the OP are not indentical and have been modified, using htmlspecialchars and (int)
If you wish to make use of htmlspecialchars, do the following in your welcome.php file:
<?php
$fname = htmlspecialchars($fname);
?>
Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo (int)$_POST['age']; ?> years old.
Form used:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
I did not see any mention on the W3Schools website about the use of htmlspecialchars or (int)
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
If you wish to make use of htmlspecialchars then you should the following syntax:
$fname = htmlspecialchars( $fname );
And placed within <?php and ?> tags such as:
<?php
$fname = htmlspecialchars( $fname );
?>
NOTE: I know next to nothing about running a Webserver from my own computer, yet from information I found here on SO
mention that in order to access your PHP files, you need to type in http://localhost in your Web browser's address bar and the folder where your file is in.
Please visit this answer
StackOverflow did not let me insert the codes on that page, for one reason or another.
In your <form> tag the "action" is where your POST data is being sent. So does your file structure look like this?
//index.php
<form action="action.php" method="POST"> // <-- make sure to capitalize method="POST" as well
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
.
//action.php
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
EDIT
Sounds like you might be getting errors in PHP that are turned off. Try this in action.php and re-submit the page.
//action.php
<?php
error_reporting(E_ALL);
?>
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
EDIT 2
Sounds like you might be getting errors in PHP that are turned off. Try this in action.php and re-submit the page.
//action.php
<?php
error_reporting(E_ALL);
?>
Hi <?php echo $_POST['name']; ?>.
You are <?php echo $_POST['age']; ?> years old.
'post' or 'POST' both works fine in form tag.
The following should be in action.php
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
If still you get this then go to php.ini and set your errors to E_ALL and E_STRICT
and check whats the error.
Most probably it should work now...
In your form check if it is not sending empty values.
STEP 1
copy and paste the following code in your text editor and run it. It will allow you to test the values from the form without redirecting the page.
The following code should be in index.php
<form action="action.php" method="POST" onsubmit="return validate()">
<p>Your name: <input type="text" name="name" id="name"/></p>
<p>Your age: <input type="text" name="age" id="age"/></p>
<p><input type="submit" /></p>
</form>
<script type="text/javascript">
function validate(){
var name=document.getElementById("name").value;
var age=document.getElementById("age").value;
alert ("Name="+name+" age="+age);
return false;
}
</script>
This code will check if the values are getting entered correctly without redirecting the page to action.php.
Step 2
If you are getting the desired output from the previous code then you can replace the validate function with the code below. (replace everything between the script tags)
function validate(){
var name=document.getElementById("name").value;
var age=document.getElementById("age").value;
if (name==null || name==""){
return false;
}
if (age==null || age==""){
return false;
}
return true;
}
If both name and age are filled in the form, the submit will now redirect to action.php
Step 3
In action.php use the following code.
<?
//These code goes in action.php
extract ($_POST);
echo "Hi $name. You are $age years old";
?>
edited with instructions on OP's request
Simply ensue that your form is running from your server (http://localhost..) and not the form location itself(file:///C:xampp..). Happy coding

PHP: get the value of TEXTBOX then pass it to a VARIABLE

Good Day!.
I need a kinda little help for PHP. I’m really really newbie for PHP and I already search it to google and can’t find any solution.
My Problem is:
I want to get the value of textbox1 then transfer it to another page where the value of textbox1 will be appeared in the textbox2.
Below is my codes for PHP:
<html>
<body>
<form name='form' method='post' action="testing2.php">
Name: <input type="text" name="name" id="name" ><br/>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
I also add the code below and the error is “Notice: Undefined index: name”
<?php
$name = $_GET['name'];
echo $name;
?>
or
<?php
$name = $_POST['name'];
echo $name;
?>
Your method is post, so use $_POST
Also, try wrapping it around an isset function:
if (isset($_POST['name'])){
echo $_POST['name'];
}
This will also handle the undefined error
This would be your textbox2:
<input type="text" name="textbox2" id="name" <?php echo (isset($_POST['name']) ? 'value=' .htmlspecialchars($_POST['name']). ' : ''); ?>/>
If you want to use the GET method, change post to get in your form and $_POST to $_GET in the php code of textbox2

Categories