How to insert the hidden field data into database using php - php

Hi While login into the site iam getting the username in hidden field while filling the form after login i need to insert the username in database.But iam getting The error as
Notice: Undefined index: email
Here is my code
index.php
<?php
$username = $_SESSION['username'];
if($username)
{
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php
}
else
{
echo "no";
}
?>
<body>
<input type='hidden' value="<?php echo $username; ?>" name='email'>
<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
<form method="post" action="details.php" id="myform">
<ul class="tab-content">
<li class="tab-pane active" id="salary">
<h3>Details</h3>
<table style="border-collapse: collapse;border: 1px solid black;">
<tr class="spaces">
<th>User Name</th></tr>
<tr>
<td><input type="text" name="user_name" value="" required /></td>
</tr>
details.php
<?php
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("accountant", $connection);
$email=$_POST['email'];
$name=$_POST['user_name'];
$query=mysql_query("INSERT INTO user_details(email,user_name)values('$email','$name')");
Can anyone help me this thanks in advance
While filling this form i need to insert the hidden field value also in database

You hidden field must be inside your form . Otherwise its value not submit form action and you will get Undefined Notice
<form method="post" action="details.php" id="myform">
<input type='hidden' value="<?php echo $username; ?>" name='email'>
--- rest code---
<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
</form>

You are getting Undefined Index notice because you are not using your hidden input inside the <form></form>
<input type='hidden' value="<?php echo $username; ?>" name='email'>
Keep in mind either filed hidden or visible you will define it inside the <form></form> otherwise you will get the Notices.
Side Note:
Please use mysqli_* or PDO extension, because mysql_* is deprecated and not available in PHP 7.
One more issue, your submit button also out side the <form> but it will work because you are using onclick() event, but its not good practice.
Your code is open for SQL Injection, you need to prevent with SQL Injection.
One last thing, i hope you are using session_start() on top in your file for getting $_SESSION['username']; in your original code, please recheck.

Try putting that field in form tag and you done.
<?php
$username = $_SESSION['username'];
if($username){?>
<h3> Welcome <?php echo $username; ?></h3>
<?php } else {
echo "no";
}
?>
<body>
<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
<form method="post" action="details.php" id="myform">
<input type='hidden' value="<?php echo $username; ?>" name='email'>
<ul class="tab-content">
<li class="tab-pane active" id="salary">
<h3>Details</h3>
<table style="border-collapse: collapse;border: 1px solid black;">
<tr class="spaces">
<th>User Name</th>
</tr>
<tr>
<td>
<input type="text" name="user_name" value="" required />
</td>
</tr>

index.php
<?php
$username = $_SESSION['username'];
if($username)
{
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php
}
else
{
echo "no";
}
?>
<body>
<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
<form method="post" action="details.php" id="myform">
<ul class="tab-content">
<li class="tab-pane active" id="salary">
<input type='hidden' value="<?php echo $username; ?>" name='email'>
<h3>Details</h3>
<table style="border-collapse: collapse;border: 1px solid black;">
<tr class="spaces">
<th>User Name</th></tr>
<tr>
<td><input type="text" name="user_name" value="" required /></td>
</tr>

<?php
session_start();
// you have to start the session in order to work with php session.
$username = $_SESSION['username'];
?>

> <form method="post" action="details.php" id="myform"> <input
> type='hidden' value="<?php echo $username; ?>" name='email'>
>
> <input type="button" value="Logout" id="logout"
> onClick="document.location.href='login.php'" />
>
> </form>
hidden type always contain hidden value

Related

How to retrieve value on from in php?

I am trying to retrieve value on php page but it's not retrieving value.
Here is my code
retrieve
<html>
<body>
<?php
include('conn.php');
$per_page = 3;
if($_GET)
{
$page=$_GET['page'];
}
$start = ($page-1)*$per_page;
$select_table = "select * from clientreg order by id limit $start,$per_page";
$variable = mysql_query($select_table);
?>
<form name="frmUser" method="post" action="">
<div style="width:100%;">
<table border="0" cellpadding="10" cellspacing="1" width="100%" class="tblListForm">
<tr class="listheader">
<td></td>
<td width="230" >*****</td>
</tr>
<?php
$i=1;
$j=0;
while($row = mysql_fetch_array($variable))
{
if($j%2==0)
$classname="evenRow";
else
$classname="oddRow";?>
<tr class="<?php echo $classname;?>">
<td><input type="checkbox" name="users[]" value="<?php echo $row["id"]; ?>" ></td>
</tr>
<?php
$j++;}
?>
<tr class="listheader">
<td colspan="9"><input type="button" name="update" id="onclick" value="Update" /> <input type="button" name="delete" value="Delete" onClick="setDeleteAction();" />
<input type="button" name="assign" id="assign" value="Assign" onClick="setLeadAssignAction();" />
<?php
$sql = mysql_query("SELECT *FROM login where role=1");
while ($row = mysql_fetch_array($sql)){
?>
<tr class="listheader">
<td><input type="checkbox" name="eid[]" value="<?php echo $row["eid"]; ?>" ><?php echo $row["username"]; ?></td>
</tr>
<?php
}
?>
</td>
</tr>
</table>
</div>
</form>
<form class="form" method ="Post"action="" id="contact">
<?php
if(isset($_POST["submit"]) && $_POST["submit"]!="") {
$rowCount = count($_POST["users"]);
for($i=0;$i<$rowCount;$i++) {
$result = mysql_query("SELECT * FROM clientreg WHERE Id='" . $_POST["users"][$i] . "'");
$row[$i]= mysql_fetch_array($result);
echo "shakti";
echo $row[$i]['id'];
}
}
?>
<img src="button_cancel.png" class="img" id="cancel"/>
<div id="left" style="height:400px;width:47%;float:left;margin-left:20px;margin-top:15px;border-radius:10px;">
<label>Lead Owner: <span>*</span></label>
<br/>
<input type="text" name="leadowner[]" id="lead" placeholder="Lead Owner"value=""/><br/>
<br/>
<label>First Name: <span>*</span></label>
<br/>
<input type="text" name="fname"id="fname" placeholder="Fname"/><br/>
<br/>
<label>Last Name: <span>*</span></label>
<br/>
<input type="text" name="lname" id="lname" placeholder="Lname"/><br/>
<br/>
<label>Mobile No: <span>*</span></label>
<br/>
<input type="text" name="mobile"id="mobile" placeholder="Mobile"/><br/>
<br/>
<label>Email Id: <span>*</span></label>
<br/>
<input type="text"name="email" id="email" placeholder="Email"/><br/>
</div>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Where am I wrong in this code?
Please sort out my problem
My problem is here
if(isset($_POST["submit"]) && $_POST["submit"]!="") {
$rowCount = count($_POST["users"]);
Any help will be appreciated
I have updated my code and added submit button.
You have two forms. Only the second form has a submit.
Form 1:
<form name="frmUser" method="post" action="">
<input type="checkbox" name="users[]" value="<?php echo $row["id"]; ?> ">
</form>
Form 2:
<form class="form" method ="Post"action="" id="contact">
<input type="submit" name="submit" value="Submit">
</form>
The if construct does not receive the $_POST["users"], because it receives only the POST of the second submitted form.
$rowCount = count($_POST["users"]);
$rowCount will always be 0.
Form 2:
...
<?php
$rowCount = 0;
if ($_POST["users"] != "") {
$rowCount = count($_POST["users"]);
} else if ($_POST["rowcount"] != "") {
$rowCount = $_POST["rowcount"];
}
?>
...
<form class="form" method ="Post"action="" id="contact">
...
<input type="submit" name="submit" value="Submit">
<input type="hidden" name="rowcount" value="<?php echo $rowCount; ?>">
</form>
Then the variable $rowCount will contain the count of the rows after the submit of any of the 2 forms.

$_Get variable not working

I'm Jad and I'm trying to make a math rounder.
The problem is that when I click Do Math!, the echo doesn't work.
Here's my code:
<?php
if(isset($_GET['math']))
switch ($_GET['math']) {
case "round":
if (isset($_POST['submit'])){
echo round($_POST['numb1']);
} else{
?>
<h1>Math Rounding!</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr>
<td>Number:</td>
<td>
<input type="text" name="numb1" placeholder="Number 1..." maxlength="44444444444444444444444444444444444444444444444444444444444444444444">
</td>
</tr>
<tr>
<th colspan=2>
<input type="submit" name="submit" value="Do Math!" style="height: 25px; width: 100px">
</th>
</tr>
</table>
</form>
</center>
<?php
break;
}
}
?>
So in each form change the value of hidden field(<input type="hidden" name="math" value="round">) acoordingly.and also need to change the forms action to a single page.
<?php
if(isset($_POST['math']))
{
switch ($_POST['math'])
{
case "round":
echo round($_POST['numb1']);
exit;
case "round1":
echo round($_POST['numb1']);
exit;
}
}
else{
?>
<h1>Math Rounding!</h1>
<form action="<?php
echo $_SERVER['PHP_SELF'];
?>" method="post">
<input type="hidden" name="math" value="round">
<table border="0">
<tr><td>Number:</td><td>
<input type="text" name="numb1" placeholder="Number 1..." maxlength="44444444444444444444444444444444444444444444444444444444444444444444">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit"
value="Do Math!" style="height: 25px; width: 100px"></th></tr></table>
</form>
</center>
<?php
}?>
You need to set method to "get" and set form action to "" instead of .There is no need for it.
Your program should work now :)
edit:
set
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
to
<form action="" method="get">

Create a back button for form

I've created a 2 step form with the following code:
<form class="form" method="POST" action="<?php bloginfo('url'); ?>/contact-us/">
<? if (!$_POST['step']) { ?>
<h1>Step 1 of 2</h1><br />
<input type="hidden" name="step" value="1" />
<table border="0" width="100%">
<tr>
<td>
<input type="text" name="title" id="title" value="Title*" />
</td>
</tr>
</table>
<button class="continue-button" type="submit" name="submit">Continue</button>
<? } else if ($_POST['step'] == 1) {
foreach($_POST as $name => $value) {
if ($name <> "step") { echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; }
} ?>
<h1>Step 2 of 2</h1><br />
<input type="hidden" name="step" value="2" />
<table border="0" width="100%">
<tr>
<td>
<input type="text" name="name" id="name" value="Name*" />
</td>
</tr>
</table>
<button class="continue-button" type="submit" name="submit">submit</button>
<? } else if ($_POST['step'] == 2) { //do stuff
echo "Do stuff here";
} ?>
</form>
How can I add a back button on step 2? There will be several steps added not just 2 so the user needs to be able to move forward and back through the steps whilst keeping what they've filled in.
Try this
<form class="form" method="POST" action="<?php bloginfo('url'); ?>/contact-us/">
<? if (!$_POST['step']) { ?>
<h1>Step 1 of 2</h1><br />
<input type="hidden" name="step" value="1" />
<table border="0" width="100%">
<tr>
<td>
<input type="text" name="title" id="title" value="<?= $_REQUEST["title"]?>" placeholder="Title*" />
</td>
</tr>
</table>
<button class="continue-button" type="submit" name="submit">Continue</button>
<? } else if ($_POST['step'] == 1) {
$field ="";
foreach($_POST as $name => $value) {
if ($name <> "step") { echo "<input type=\"hidden\" name=\"$name\" value=\"$value\" />"; $field .= $name."=".$value."&";}
} ?>
<div><a href="<?php bloginfo('url'); ?>/contact-us/?<?= $field ?>" >Back</a></div>
<h1>Step 2 of 2</h1><br />
<input type="hidden" name="step" value="2" />
<table border="0" width="100%">
<tr>
<td>
<input type="text" name="name" id="name" value="Name*" />
</td>
</tr>
</table>
<button class="continue-button" type="submit" name="submit">submit</button>
<? } else if ($_POST['step'] == 2) { //do stuff
echo "Do stuff here";
} ?>
</form>
First add it on top of your page
// keep the data
$_SESSION['data'] = $_POST;
$_SESSION['step'] = $_REQUEST['step'];
Also replace the if statement to something like this:
if ($_REQUEST['step'] == 1) {
// continue
Write dynamic values on your form
<input type="text" name="title" id="title"
value="<?php echo $_SESSION['data']['title']; ?>" />
Use a link for Back and Next:
Back
Next
I guess it would works for you! :)
To navigate back and forwards you will want to pass the data from the previous form back to it again so you would need to store the value of each form somewhere. Your best bet would be to create a session and store each form info in a session variable. Then when you load each form you should check to see if the data is already stored in a session.
Then you could add a jquery button to change the value of the step and submit the form.
To do this you would need to the 'step' hidden input an id like 'step'
<input type="hidden" name="step" id="step" value="2" />
and then add a button like
<button class="back-button" type="submit" name="back" onclick="$('#step').val('1');">Back</button>
This would change the value of the step input and then submit the form taking you back to the previous form.
You would need to have another hidden input to tell the form where it came from so it would know whether to look at the session data (is you're going backwards) or the POST data (if youre going forwards).

can't i display two session message in one page

i have been displaying two session messages in one page, rating. One message is displayed in every refresh showing the likes and dislikes. the other message in displayed once the rating form is submitted, the problem is once i submit the rating form the message displaying the rating isn't displayed. why is so??? can't i use two sessions in a page?? thanks
coding
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?
session_unset($_SESSION['error_msg']);
}
?>
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?>
<form name="rating_form" action="" method="post">
<input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
<input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
<div style="text-align: center">
<input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
<input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
</div>
</form>
sorry i think my code is a bit confusing
my actual code looks like this
<div class="top_model">
<h1>Model Ratings</h1>
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?
session_unset($_SESSION['error_msg']);
}
?>
<ul class="homemodel_thumbs">
<div style="text-align: right">
</div>
<?php
if(!empty($ratingArr)){
foreach($ratingArr as $ratings){
?>
<li><img src="<?php echo base_url();?>images/gallery/large/<?php echo $ratings['gallery_image'];?>" alt="<?php echo $ratings['gallery_image'];?>" class="center_image"/>
<p> </p>
<?php
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
//session_unset($_SESSION['rate_msg']);
}
?>
<form name="rating_form" action="" method="post">
<input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
<input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
<div style="text-align: center">
<input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
<input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
</div>
</form>
</li>
<?php
}
}
?>
</ul>
this problem is solved. of the two session message for the one i used php session while for the another one i used code igniter session. sorry if i haven't mentioned i am using ci. anyways thanks everyone for the help :)
You're unsetting your rating message after you display it:
session_unset($_SESSION['rate_msg']);
This means it's not available after you've posted back the form unless you re-populate it again.
Remove this and i suspect it'll work.
You have an excess ?> :
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?
session_unset($_SESSION['error_msg']);
}
if(!empty($_SESSION['rate_msg']))
{
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?>
You don't open php tag for last statement, (or unnesesery close it) it's treated as html
<?php
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?php
session_unset($_SESSION['error_msg']);
}
?>
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?>
EDIT
<?php
// < HERE IS PHP >
if(!empty($_SESSION['error_msg'])){?>
<div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<!-- HERE IS HTML -->
<?
// < HERE IS PHP >
session_unset($_SESSION['error_msg']);
}
?> <!-- CLOSED PHP - NOW IT'S HTML - YOU SHOULD REMOVE IT -->
<!-- THIS IS NOT EXECUTED - BECAUSE IT'S HTML NOW -->
if(!empty($_SESSION['rate_msg'])){
echo $_SESSION['rate_msg'];
session_unset($_SESSION['rate_msg']);
}
?> <!-- THIS IS NOT PHP TAG BECASUE THERE IS NOT OPEN TAG -->
<form name="rating_form" action="" method="post">
<input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
<input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
<div style="text-align: center">
<input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
<input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
</div>
</form>

nor $_POST, $_GET passing my values

I have a foreach loop listing the unique codes as links:
<a href="#" class="charcoal_link" value="<?php echo $id_to ?>" style="line-height: 20px;" onclick="showMessageArea(this); return false;">
<?php echo $uniqueCode1?>
<span class="pink_text"><?php echo $uniqueCode2?></span>
<?php echo $uniqueCode3?>
</a>
<input type="hidden" name="id" value="<?php echo $id_to ?>" />
<form id="message_area" style="display:none" method="post" action="<?php echo ADDRESS; ?>messageSent.php?id=<?php echo $id_to ?>">
<tr>
<td>
<input name="message" type="textarea" rows="10" cols="20" value="<?php echo $message ?>" />
</td>
<td>
<input name="Submit" type="submit" value="Send" />
</td>
</tr>
</form>
the above is also in another form tag: <form name = "contacts" method="post">
the action part works fine but in messageSent.php i do:
var_dump($_POST);
var_dump($_GET);
and the output gives me:
NULL
array(1) { ["id"]=> string(2) "36" }
Where is "message"?
"id" contains the wrong id. It is not the id of the unique code I clicked on.
Your HTML is invalid. Among the many errors you have made: You can't have a form inside a table but around a tr. The browser is error correcting by moving the form element to somewhere where it is allowed, and leaving the inputs alone (since inputs can be inside td elements).
Validate. Validate. Validate.
There are so many problems with that fragment of HTML that you have shared, that you would probably benefit from a beginner's guide to authoring webpages.
<a href="#" class="charcoal_link" value="<?php echo $id_to ?>" style="line-height: 20px;" onclick="showMessageArea(this); return false;">
<?php echo $uniqueCode1?>
<span class="pink_text"><?php echo $uniqueCode2?></span>
<?php echo $uniqueCode3?>
</a>
<form id="message_area" style="display:none" method="post" action="<?php echo ADDRESS; ?>messageSent.php?id=<?php echo $id_to ?>">
<input type="hidden" name="id" value="<?php echo $id_to ?>" />
<table>
<tr>
<td>
<textarea name="message" rows="10" cols="20"><?php echo $message ?></textarea>
</td>
<td>
<input name="Submit" type="submit" value="Send" />
</td>
</tr>
</table>
</form>
You cannot have a form inside a form.. you must do them seperate or else in the same form....
I fixed the HTML markup for you

Categories