Undefined index in post php - php

I have a form code php
<form action="CR/add.php" method="post">
<div class="form-group">
<label>Date</label>
<input id="date" type="text" name="date" class="date" value="<?php echo $datenow; ?>" required <?php if($user!="admin"){echo "disabled";}?>>
</div>
<button type="submit" class="btn btn-primary"><span class="fa fa-edit"></span> Input Data</button>
</form>
I get some error when I click the submit button. The error is Undefined index variable $date. I want only admin users to be able to edit the fill.
Why am I getting this error, and how can I fix it?
Any help would be appreciated.

You did not include the related PHP code for where the $date variable is declared or assigned a value.
A good way to do this would be to check if the variable was supplied using isset http://php.net/manual/en/function.isset.php
Example:
$date = '';
if(isset($_POST['date'])){
$date = $_POST['date'];
}
** Will need to see more of your code to give a more specific example.

# Kurnia Rocki - Just replace your code with this:
<script>
function check()
{ var user = document.getElementById('user').value;
if(user!='admin')
{ alert('Only admin can update');
return false;
}
else
return true;
}
</script>
<form action="CR/add.php" method="post" onSubmit="return check()">
<div class="form-group">
<label>Date</label>
<input id="date" type="text" name="date" class="tanggal" value="<?php echo $datenow; ?>" >
</div>
<input id="user" type="text" name="user" value="<?php echo $user; ?>" style="display:none;">
<button type="submit" class="btn btn-primary"><span class="fa fa-edit">
</span>Input Data</button>
</form>

Related

Cannot display edit form when clicked on edit button and the form doesn't parse error and displays blank page

EDIT: My edit button refuse to work.
I'm trying to edit this page, but it seems to be breaking when I click the edit button. it is retrieving data from the database and displaying on faculty.php successfully while if I try to edit it shows a blank page. what is wrong with my code, please.
faculty.php: a separate page where i have my edit button
<td>
<form action="facultyedit.php" method="POST">
<input type="hidden" name="edit_id" value="<?php echo $row['id']; ?>" >
<button type="submit" name="edit_data_btn" class="btn btn-success">EDIT</button>
</form>
</td>
facultyedit page: where i receive the edit button
<?php
if (isset($_POST['edit_data_btn']))
{
$id = $_POST['edit_id'];
$query = " SELECT * FROM register WHERE id='$id' ";
$query_run = mysqli_query($connection, $query);
foreach ($query_run as $row ) {
?>
<form action="" method="POST" >
<input type="text" name="edit_id" value="<?php echo $row['id'] ?>" >
<div class="form-group">
<label>Name:</label>
<input type="text" name="edit_name" value="<?php echo $row['name']; ?>" class="form-control" >
</div>
<div class="form-group">
<label>Designation:</label>
<input type="text" name="edit_designation" value="<?php echo $row['design']; ?>" class="form-control" >
</div>
<div>
<label>Description:</label>
<input type="text" name="edit_description" value="<?php echo $row['descrip']; ?>" class="form-control" >
</div>
<div class="form-group">
<label>Upload Image:</label>
<input type="file" name="edit_faculty_image" id="faculty_image" value="<?php echo $row['image']; ?>" class="form-control" >
</div>
<div class="">
Cancel
<button type="update" name="update_aboutusbtn" class="btn btn-primary">Update</button>
</div>
</form>
<?php
}
}
?
I have fixed the issue:
The problem was that I was selecting a register table from the database instead of the faculty table.

Passing variable value to URL

I want to pass a variable value to an url but the problem is when I first clicked the search button the value doesn't appear and when the second clicked is triggered the variable value is shown. How can i fix this? I think the fault is when i click the search button the page is refreshing. Can someone help me about this?
here's my code:
<div class="form-inline form-padding">
<form action="student.php?classid=<?php echo $classid;?>" method="post">
<input type="text" class="form-control" name="search" placeholder="Search by ID or Name">
<select name="subject" class="form-control" required>
<option value="">Select Subject...</option>
<?php while($row = mysql_fetch_array($mysubject)): ?>
<option value="<?php echo $row['id']?>" <?php if($row['id']==$classid) echo 'selected'; ?>><?php echo $row['subject'];?></option>
<?php endwhile; ?>
</select>
<button type="submit" name="submit" id="submit" class="btn btn-primary"><i class="fa fa-search"></i> Search</button>
<button type="button" name="submit" class="btn btn-success"><i class="fa fa-print"></i> Print</button>
</form>
</div>
in your
<form action="student.php?classid=<?php echo $classid;?>" method="post">
change it to
<form action="student.php" method="post">

Modify form input value on submit

I wasn't sure how to phrase the title, but here's what I'm trying to do.
I have a form to login to webmail, I don't have access to the webmail - it just posts the form input to the website and the webmail server takes it from there.
Everyone logging in to this page will be using the same email extension (e.g. "#myemail.com"), so I want to save the hassle of typing that every time, instead they can just write "mike" and the form will add "#myemail.com" on it's own.
I could post this form to a middle ground php page that sticks "mike" + "#mymail.com" together and posts this info to the webmail?
Or is there a simpler way to do this without having to create another page?
<form name="loginForm" action="http://webmail.emailsrvr.com/login.php" method="POST" target="_blank">
<input type="hidden" name="js_autodetect_results" value="SMPREF_JS_OFF">
<input type="hidden" name="just_logged_in" value="1">
<input type="hidden" name="type" value="v3">
<input type="hidden" name="useSSL" id="useSSL" value="">
<input type="email" name="user_name" placeholder="Username" required autofocus>
<input type="password" name="password" placeholder="Password" required>
<label for="remember"><input type="checkbox" name="remember">Remember my info</label>
<button type="submit">Sign In</button>
</form>
I want to take the value entered here...
<input type="email" name="user_name" placeholder="Username" required autofocus>
...and add an extension like "#myemail.com" to it, then post it.
Any idea? Thanks in advance for your help.
I could do something like this?
PAGE 1 - Enter username and password...
<input type="text" name="send_email" placeholder="Username" required autofocus>
<input type="password" name="send_password" placeholder="Password" required>
<label for="remember"><input type="checkbox" name="remember">Remember my info</label>
<button type="submit">Sign In</button>
</form>
PAGE 2 - PHP takes username and adds "#myemail.com" and sends form...
<?php
$form_email = $form_password = "";
if (!empty($_POST['send_email']) && !empty($_POST['send_password'])) {
$form_email = $test($_POST['send_email']) . '#fountaincreations.ca';
$form_password = $test($_POST['send_password']);
echo '<form name="loginForm" action="http://webmail.emailsrvr.com/login.php" method="POST" target="_blank">';
echo '<input type="hidden" name="js_autodetect_results" value="SMPREF_JS_OFF">';
echo '<input type="hidden" name="just_logged_in" value="1">';
echo '<input type="hidden" name="type" value="v3">';
echo '<input type="hidden" name="useSSL" id="useSSL" value="">';
echo '<input type="hidden" name="user_name" value="' . $form_email . '">';
echo '<input type="hidden" name="form_password" value="' . $form_password . '">';
echo '</form>';
} else {
echo '<p style="text-align:center;padding:40px 20px;">Please go back and try again.</p>';
}
function test($data) {
$data = strtolower(data);
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
How do I tell PHP to automatically submit the form though?
Here's what I have if I were to use jQuery:
It doesn't currently work though.
HTML:
<div class="box top">
<form name="loginForm" action="http://webmail.emailsrvr.com/login.php" method="POST" target="_blank">
<input type="hidden" name="js_autodetect_results" value="SMPREF_JS_OFF">
<input type="hidden" name="just_logged_in" value="1">
<input type="hidden" name="type" value="v3">
<input type="hidden" name="useSSL" id="useSSL" value="">
<div class="inner">
<div class="inner-row"><input type="text" name="user_name" placeholder="First Name" required autofocus></div>
<div class="inner-row"><input type="password" name="password" placeholder="Password" required></div>
<div class="inner-row">
<div class="inner-col col-2-4"><label for="remember"><input type="checkbox" name="remember">Remember my info</label></div>
<div class="inner-col col-2-4"><button type="submit">Sign In</button></div>
</div>
</div>
</form>
</div>
<div class="box bottom">
<div class="inner">
<div class="inner-row"><p>Already signed in? <b>Go to your inbox.</b></p>
</div>
</div>
JS:
<script type="text/javascript">
$(document).ready(function(){
$("button[type='submit']").on('click', function(e){
e.preventDefault();
var userEmail = $.trim($("input[name='user_name']").val());
$("input[name='user_name']").val(userEmail+"#fountaincreations.ca");
$("form[name='loginForm']").submit();
});
});
</script>
I think if you want to attach username + "#mymail.com" to useremail field, here is my suggestion:
1# create input user_email (just hide this field)
<input type="hidden" name="user_email" id="user_email" placeholder="User Email">
2# add js below
$('#user_name').on('input', function() {
username = this.value+'#myemail.com';
$('#user_email').val(username);
});
here example
I think this will go like this, hope this will help you. What i am doing in this code is, first I stop the form from being submitted through e.preventDefault(); and then i am taking the value of the required input and assigning it back value by attaching "#mymail.com".
$(document).ready(function(){
$("button[type='submit']").on('click', function(e){
e.preventDefault();
var userEmail = $.trim($("input[type='email']").val());
$("input[type='email']").val(userEmail+"#mymail.com");
$("form[name='loginForm']").submit();
});
});

PHP two forms, one page

So in school we're learning about OOP in PHP and for our assignment we need to use 2 forms. this is the first time I'm using 2 forms in one page and I've been trying to figure out how to check which form is being submitted and create the according object.
Apparently, afer looking at some other questions, just using if (!empty($_POST['SubmitButtonName'])) should work, but it doesnt.
Hope someone can help me and tell me what I'm doing wrong :)
PHP:
if (!empty($_POST['sportwgn']))
{
try
{
$sport->Merk = $_POST['merk'];
$sport->AantalPassagiers = $_POST['AantalPassagiers'];
$sport->AantalDeuren = $_POST['AantalDeuren'];
$sport->Stereo = isset($_POST['stereo']) ? true : false;
$sport->Save();
$succes= "Uw sportwagen is gereserveerd!";
}
catch( Exception $e)
{
$error = $e->getMessage();
}
}
if (!empty($_POST['vrachtwgn']))
{
try
{
$vracht->Merk = $_POST['merk'];
$vracht->AantalPassagiers = $_POST['AantalPassagiers'];
$vracht->AantalDeuren = $_POST['AantalDeuren'];
$vracht->MaxLast = $_POST['MaxLast'];
$vracht->Save();
$succes= "Uw vrachtwagen is gereserveerd!";
}
catch( Exception $e)
{
$error = $e->getMessage();
}
}
Forms:
<form action="" method="post">
<label for="merk">merk</label>
<input type="text" id="merk" name="merk">
<br>
<label for="AantalPassagiers">Aantal passagiers</label>
<input type="number" min="2" max="4" id="AantalPassagiers" name="AantalPassagiers">
<br>
<label for="AantalDeuren">Aantal deuren</label>
<input type="number" min="1" max="5" id="AantalDeuren" name="AantalDeuren">
<br>
<label for="stereo">Stereo?</label>
<input type="checkbox" name="stereo" id="stereo" value="stereo"><br>
<br></div><div class="box">
<button type="submit" name="sportwgn">Reserveer</button></div>
</form>
</div>
</div>
<div id="container">
<h1 class="box">Reserveer een Vrachtwagen!</h1>
<div id="content">
<form action="" method="post">
<label for="merk">merk</label>
<input type="text" id="merk" name="merk">
<br>
<label for="AantalPassagiers">Aantal passagiers</label>
<input type="number" min="2" max="4" id="AantalPassagiers" name="AantalPassagiers">
<br>
<label for="AantalDeuren">Aantal deuren</label>
<input type="number" min="1" max="5" id="AantalDeuren" name="AantalDeuren">
<br>
<label for="MaxLast">Max last</label>
<input type="number" min="1" max="5" id="MaxLast" name="MaxLast"><br>
<br></div><div class="box">
<button type="submit" name="vrachtwgn">Reserveer</button></div>
</form>
Since your forms post on the same page (...action=""...) divide your code in php side to two actions by the submit button.
for the forms with
<button type="submit" name="sportwgn">Reserveer</button></div>
use
if(isset($_POST['sportwgn'])) {
// your code
}
and for
<button type="submit" name="vrachtwgn">Reserveer</button></div>
use
if(isset($_POST['vrachtwgn'])) {
// your code
}
You can use if(isset($_POST['buttonName'])) to check if it's in the post values.
Use action attribute to submit forms to different destinations.
<form action="firstForm.php" method="post">
...
</form>
<form action="secondForm.php" method="post">
...
</form>
And create 2 files for handling form posting.

Can't retrieve values from other page (Stored As Label)

I am new to PHP and I am trying to get information from a form to another page, but the data won't transfer over when I hit submit. What am I doing wrong? Should I be trying to use GET instead of POST? What is the best way to debug something like this?
The path to information.php is definitely correct.
<form action="information.php" method="post" type="post">
<div class="row" style="padding-bottom: 20px;">
<label name="tempID"><?php echo $number; ?></label>
<button class="btn" name="submit" type="submit">More Details</button>
</div>
</form>
This file is in a different page (information.php)
if (isset($_POST["tempID"]))
{
$infoID = $_POST['tempID'];
}
echo $infoID;
<input type="hidden" name="tempID" value="<?php echo $number; ?>" />
Add this next to your original echo, post variables can't be stored in a label. Also remove the name value from the label
You need a submit input instead of a button with the name submit. Change the button's html to:
<input type='submit' value='More Details'>
Change Label in input
<form action="information.php" method="post">
<div class="row" style="padding-bottom: 20px;">
<input name="tempID" value="<?php echo htmlentities($number); ?>"/>
<button class="btn" name="submit" type="submit">More Details</button>
</div>
</form>
labels are only to display information. they are not submitted during form submission.
<form action="information.php" method="post" type="post">
<div class="row" style="padding-bottom: 20px;">
<label>Number:</label>
<input name="tempID" value="<?php echo $number; ?>"/>
<input class="btn" id="submit" name="submit" type="submit" value="More Detail" />
</div>
</form>

Categories