WordPress : Form data is not displaying via echo - php

I have created a form to which I am passing the data. When I try to echo the data it shows couple of errors saying that index is undefined.
<?php /* Template Name: Dummy Practice Page*/?>
<div id="main-content" class="main-content">
<div class="main-content-inner">
<form method='post' enctype='multipart/form-data'>
<input id="name" type="text" style="height:30px; width: 350px; " maxlength="5" placeholder="Name" required><br>
<input id="designation" type="text" style="height:30px; width: 350px; " maxlength="50" placeholder="Designation" required><br>
<input id="description" type="text" style="height:30px; width: 350px; " maxlength="1000" placeholder="Description" required><br>
<input id="pic" type="file" style="height:30px; width: 350px; "><br><br>
<input name="insert" type='submit' style="height:40px; width: 130px; padding:10px; color:dodgerblue; background-color:black; border-radius:20px; " name='Submit' value='Add Member' /><br><br>
</form>
</div>
</div>
<?php
if(isset($_POST['insert']))
{
echo $namevar = isset($_POST['name']);
echo $descriptionvar = isset($_POST['description']);
echo $designationvar = isset($_POST['designation']);
}
?>
Following are the errors:
Notice: Undefined index: name in C:\xampp\htdocs\wordpress\wp-content\themes\twentyfifteen-child\practicepage.php
Notice: Undefined index: description in C:\xampp\htdocs\wordpress\wp-content\themes\twentyfifteen-child\practicepage.php
Notice: Undefined index: designation in C:\xampp\htdocs\wordpress\wp-content\themes\twentyfifteen-child\practicepage.php

<form method='post' enctype='multipart/form-data'>
<input name="name" type="text" style="height:30px; width: 350px; " maxlength="5" placeholder="Name" required><br>
<input name="designation" type="text" style="height:30px; width: 350px; " maxlength="50" placeholder="Designation" required><br>
<input name="description" type="text" style="height:30px; width: 350px; " maxlength="1000" placeholder="Description" required><br>
<input id="pic" type="file" style="height:30px; width: 350px; "><br><br>
<input name="insert" type='submit' style="height:40px; width: 130px; padding:10px; color:dodgerblue; background-color:black; border-radius:20px;" value='Add Member' /><br><br>
</form>
<?php
if(isset($_POST['insert']))
{
echo $namevar = $_POST['name'];
echo $descriptionvar = $_POST['description'];
echo $designationvar = $_POST['designation'];
}
?>

The issue is with the names of input fields.
In PHP or any other programming language, You should use the input name="something" to get the input values.
You should give the name for each input fields.
For example,
<input name = "name" id="name" type="text" style="height:30px; width: 350px; " maxlength="5" placeholder="Name" required><br>

You cannot pass POST data to another page vie using ID.
You should give name to <input>
<input name="name">
<input name="description">
<input name="designation">
Edit
You are defining variables and echoing them same time
Change your code to:
<?php
if(isset($_POST["insert"])){
echo $_POST["name"];
echo $_POST["description"];
echo $_POST["designation"];
}?>

Related

I am trying to get value from radioButton but something happening wrong

I have tried so many times in different ways to solve this. Every time it generates the same output. I searched in [http://google.com] and [http://youtube.com] and followed many tutorials, and implemented my code according them..Their code runs properly but my code gives unknown output, though their code and my code is same.
This is the PHP code:
if(!isset($_POST["radio"])){
$radioMsg="You must Select a Category!";
}
if(isset($_POST["radio"])){
$radio=$_POST["radio"];
}
There is the radio Buttons
<form method="post" action="register.php" enctype="multipart/form-data"
style="width: 860px;margin: 0px;height: 580px;">
<input class="form-control" type="" name="name" placeholder="Name..."/
style="width: 400px; margin-left: 200px" ><br>
<input class="form-control" type="email" name="email"
placeholder="Email..."/ style="width: 400px; margin-left: 200px" ><br>
<input class="form-control" type="password" name="password"
placeholder="Password"/ style="width: 400px; margin-left: 200px" ><br>
<input class="form-control" type="password" name="cPassword"
placeholder="
confirm Password..."/ style="width: 400px; margin-left: 200px" ><br>
<input class="form-control" type="text" name="contactNO"
placeholder="Your
contact No.."/ style="width: 400px; margin-left: 200px" ><br>
<input type="file" name="image"/ style="width: 400px; margin-left:
200px">
<br><br>
<div style="border: 1px solid #4C6A6D;width: 250px;margin-left: 200px" >
<?php echo $radioMsg ?>
<h6 style="margin-left: 20px">Select a Cetagory</h6>
<input type="radio" name="radio" value="customer" style="margin-left:
25px">Customer<br>
<input type="radio" name="radio" value="DespensaryOwner" style="margin-
left:
25px">Despensary Owner<br>
input type="radio" name="radio" value="dealer" style="margin-left:
25px">Dealer<br>
</div>
<br>
<input class="btn-primary" type="submit" name="submit" value="Register"
style=" margin-left: 200px"><br><br>
</form>
if select radioButton1, it returns a value "radio" instead of value "customer"
Input type cannot be "cust":
<input type="radio" name="g" value="customer" style="margin-left:
25px">Customer<br>
<input type="radio" name="g" value="DespensaryOwner" style="margin-left:
25px">Despensary Owner<br>
<input type="radio" name="g" value="dealer" style="margin-left:
25px">Dealer<br>enter code here
Try code above and see if that works, also you should use better name values as suggested in the comments of this answer
I went ahead and ran this locally, by putting it all in a file called "register.php" in the root of a PHP dev server instance, with the PHP code at the top. In that situation, it spits out the error until a valid value is put in, but I imagine you're handling things differently.
I had to remove a number of strange forward slashes and line returns in your HTML, there were missing < and >, and your radio buttons were all named "radio", but after correcting those errors, the code below works:
PHP: (Included PHP tags for completeness)
<?php
if(!isset($_POST["category"])){
$radioMsg="You must Select a Category!";
}
if(isset($_POST["category"])){
$radioMsg="";
$radio=$_POST["category"];
}
?>
HTML: (Included entire html layout for completeness.)
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="register.php" enctype="multipart/form-data" style="width: 860px;margin: 0px;height: 580px;">
<input class="form-control" type="" name="name" placeholder="Name..." style="width: 400px; margin-left: 200px" ><br>
<input class="form-control" type="email" name="email" placeholder="Email..." style="width: 400px; margin-left: 200px" ><br>
<input class="form-control" type="password" name="password" placeholder="Password" style="width: 400px; margin-left: 200px" ><br>
<input class="form-control" type="password" name="cPassword" placeholder="confirm Password..." style="width: 400px; margin-left: 200px" ><br>
<input class="form-control" type="text" name="contactNO" placeholder="Your contact No.." style="width: 400px; margin-left: 200px" ><br>
<input type="file" name="image" style="width: 400px; margin-left: 200px">
<br><br>
<div style="border: 1px solid #4C6A6D;width: 250px;margin-left: 200px" >
<?php echo $radioMsg ?>
<h6 style="margin-left: 20px">Select a Category</h6>
<input type="radio" name="category" value="customer" style="margin-left: 25px">Customer<br>
<input type="radio" name="category" value="DispensaryOwner" style="margin-left: 25px">Dispensary Owner<br>
<input type="radio" name="category" value="dealer" style="margin-left: 25px">Dealer<br>
</div>
<br>
<input class="btn-primary" type="submit" name="submit" value="Register"
style=" margin-left: 200px"><br><br>
</form>
</body>
</html>

Pre fill form using the URL bar

I have a form which I need it to insert data using the URL first for a few fields. I've attached a sample of my form below. We're posting it using PHP into a mySQL database. I don't mind how it gets in there. But i've seen examples using example.com/form.php?add_1=10 The Street, which would be the best way for me.
Thanks,
<form role="form" method="post" action="<?php $_SERVER['PHP_SELF'] ?>" name="epicform">
<input type="text" class="form-control" id="lastname" name="surname" required style="
height: 35px;
">
<input type="text" class="form-control" id="add_1" name="add1" placeholder="Address Line 1" required style="
height: 35px;
">
<input type="text" class="form-control" id="add_2" name="add2" placeholder="Address Line 2" required style="
height: 35px;
">
<input type="text" class="form-control" id="add_3" name="add3" placeholder="Address Line 3" required style="
height: 35px;
">
<input type="text" class="form-control" id="postcode" name="postcode" placeholder="Postcode" required style="
height: 35px;
">
<input type="text" class="form-control" id="phone_number" name="phoneNum" placeholder="Phone Number" required style="
height: 35px;
">
</form>
I've already tried a javascript solution on here, but I couldn't seem to get it to do anything.
<form role="form" method="post" action="<?php $_SERVER['PHP_SELF'] ?>" name="epicform">
<input type="text" class="form-control" id="add_1" name="add1" value="<?php echo $_GET['add1']; ?>" placeholder="Address Line 1" required style="height: 35px;">
</form>
Now your URL must work!
http://example.com/form.php?add_1=10
What you need is this: http://www.php.net/manual/en/reserved.variables.get.php
http://urlToYourScript.php?add1=Randomstreet
<input type="text" class="form-control" id="add_1" name="add1"
placeholder="<?php echo $_GET['add1']; ?>" required style="height: 35px;">

PHP Form GET doesn't work with other variabeles in URL

I wanted to make a search form with GET but there seems to be a problem. The page you are looking at has other GET values too but it seems that the GET method clears the URL from variabels en goes like "?q=SEARCH" instead of "?p=Test&u=Bart&q=SEARCH".
This is the form code I'm using:
<form method="GET" action="test.php?p=glistentries&maingroup=<? echo $MainGroupID ?>&subgroup=<? echo $SubGroupID ?>">
<input type="text" class="form-control" id="focusedinput" value="<? echo $_GET['q']; ?>"
name="q" autocomplete="off" class="col-sm-4 control-label" placeholder="Search...">
<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;"/>
</form>
Solved It
Thanks guys, forgot I can simply do this with hidden fields
Add the additional GET vars as hidden inputs:
<input type="hidden" name="p" value="Test">
Use input type hidden :
<form method="GET" action="test.php">
<input type="text" class="form-control" id="focusedinput" value="<? echo $_GET['q']; ?>" name="q" autocomplete="off" class="col-sm-4 control-label" placeholder="Search...">
<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;"/>
<input type="hidden" name="p" value="glistentries" />
<input type="hidden" name="maingroup" value="<? echo $MainGroupID ?>" />
<input type="hidden" name="subgroup" value="<? echo $SubGroupID ?>" />
</form>

PHP $_POST returns null values

This is my html code:
<form style="width: 20%; margin: auto;" action="subscribe.php" method="post" id="subscribeToNews">
<fieldset>
<legend>Subscribe:</legend>
<label for="subName">First Name:</label><br /><input type="text" id="subName" name="subName" /><br />
<label for="subEmail">Email:</label><br /><input type="text" id="subEmail" name="subEmail" /><br />
<input style="width: inherit;" type="submit" value="Subscribe" />
</fieldset>
</form>
This is the subscribe.php file:
<?php
$con = mysqli_connect('95.76.197.98','root','','accounts');
print_r($_POST);
if (isset($_POST["subName"]) && isset($_POST["subEmail"])){
$subUser = $_POST["subName"];
$subEmail = $_POST["subEmail"];
echo "$subUser"."<br />"."$subEmail";
}
?>
I have really tried a lot of things out there on the Internet and nothing seems to work for me. Any ideas?
Also looks like the get method works for this...
Could by related to your nginx configuration.
Try:
$post = file_get_contents("php://input");
This should work:
<form style="width: 20%; margin: auto;" action="subscribe.php" method="post" id="subscribeToNews">
<fieldset>
<legend>Subscribe:</legend>
<label for="subName">First Name:</label><br /><input type="text" id="subName" name="subName" /><br />
<label for="subEmail">Email:</label><br /><input type="text" id="subEmail" name="subEmail" /><br />
<input style="width: inherit;" type="submit" value="Subscribe" />
</fieldset>
</form>
<?php
$con = mysqli_connect('95.76.197.98','root','','accounts');
if($_POST) {
$subName = mysqli_real_escape_string($con, strip_tags($_POST['subName']));
$subEmail = mysqli_real_escape_string($con, strip_tags($_POST['subEmail']));
if(isset($subName) && !empty($subName) && isset($subEmail) && !empty($subMail)) {
echo 'Name: '.$subName.'<br> Email: '.$subEmail;
}
}
?>

Unable to Style Div

Creating a blog from scratch is a difficult process but I've been plodding along with some minor issues which have been resolved. My minor issue today is the fact that I'm unable to style the site background with an image. I'm able to add colors but not an image.
The second minor issue is to do with my Div tags. I've created a basic form that submits comments to my MySQL database but when I attempt to style the div that I've enclosed the form and comments that are displayed the styles have not appeared. I have included my Comments CSS below and my post.php page.
CSS:
#comments-title {
background-color: #282828;
width: 567px;
height: 30px;
font-size: 25px;
font-family: arial;
color: #ffffff;
padding: 5px;
padding-top: 6px;
padding-bottom: 4px;
}
#comment-list{
border:1px solid #dadada;
width: 567px;
padding: 5px;
}
PHP:
<h2 id="comments-title">Comments</h2>
<div id="comment-list'">
<?php
}
$commenttimestamp = strtotime("now");
$sql = "SELECT * FROM php_blog_comments WHERE entry='$id' ORDER BY timestamp";
$result = mysql_query ($sql) or print ("Can't select comments from table php_blog_comments.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$timestamp = date("l F d Y", $row['timestamp']);
print("<p id='comment'>" . stripslashes($row['comment']) . "</p>");
printf("<p id='comment'>Comment by %s # %s</p>", stripslashes($row['url']), stripslashes($row['name']), $timestamp);
}
?>
<form method="post" action="process.php">
<p><input type="hidden" name="entry" id="entry" value="<?php echo $id; ?>" />
<input type="hidden" name="timestamp" id="timestamp" value="<?php echo $commenttimestamp; ?>">
<strong><label for="name">Name:</label></strong> <input type="text" name="name" id="name" size="25" /><br />
<strong><label for="email">E-mail:</label></strong> <input type="text" name="email" id="email" size="25" /><br />
<strong><label for="url">URL:</label></strong> <input type="text" name="url" id="url" size="25" value="http://" /><br />
<strong><label for="comment">Comment:</label></strong><br />
<textarea cols="25" rows="5" name="comment" id="comment"></textarea></p>
<p><input type="submit" name="submit_comment" id="submit_comment" value="Add Comment" /></p>
</form>
</div>
Take a second look at this line:
<div id="comment-list'">
See that little ' in there? Take it out.
For everything else, you're going to have to be more specific. You've basically posted a large slab of code here without pointing to the offending code.
Remember that the ID have to be unique. use class instead if id. (In your while loop: <p id='comment'>, you have multiple id's with the same id.
You have a lot of this to fix.
And, try to validate your code to check for some errors that might prevent it from working. http://validator.w3.org
+ You have a single quote in your comment-list ID

Categories