My Form Look like this, here i wanted to send grade of Nepali subject database by taking and converting numeric marks from form mepali_th_num but what ever input numeric mark are it only send 'E' Grade to database after conversion of grade. What is wrong in this code mainly in if else part.
<form class="form-horizontal" method="post">
<div class="box-body">
<div class="form-group">
<label for="nepali_th_num" class="col-sm-2 control-label">Nepali(TH)
</label>
<div class="col-sm-2">
<input type="number" class="form-control" id="nepali_th_num"
name="nepali_th_num" step="any" min="0">
</div>
</div>
<div class="box-footer">
<button type="submit" name="btnSave" class="btn btn-
primary">Submit</button>
<button type="reset" class="btn btn-info">Reset</button>
</div>
</form>
my php look like this:
if (isset($_POST['btnSave'])){
$marksdetails = new Marksdetails();
$marksdetails->set('nepali_th_num', $_POST['nepali_th_num']);
$nep_per=((('nepali_th_num')*100)/75);
if($nep_per>=90){
$nepali_th_grade='A+';
}elseif ($nep_per>=80 && $nep_per<90) {
$nepali_th_grade='A';
}elseif ($nep_per>=70 && $nep_per<80){
$nepali_th_grade='B+';
}elseif($nep_per>=60 && $nep_per<70) {
$nepali_th_grade='B';
}elseif($nep_per>=50 && $nep_per<60){
$nepali_th_grade='C+';
}elseif($nep_per>=40 && $nep_per<50){
$nepali_th_grade='C';
}elseif($nep_per>=30 or $nep_per<40){
$nepali_th_grade='D+';
}elseif($nep_per>=20 or $nep_per<30){
$nepali_th_grade='D';
}else{
$nepali_th_grade='E';
}
$marksdetails->set('nepali_th_grade', $nepali_th_grade);
$marksdetails->set('symbol_number', $symbolnumber);
$marksdetails->set('student_name', $studentname);
$status=$marksdetails->checkDuplicate(); //to check duplicate and sql part
is in this
}
nepali_th_num in this line
$nep_per=((('nepali_th_num')*100)/75);
is just a string, its not a variable containing something useful
Try
$nep_per=((($_POST['nepali_th_num'])*100)/75);
Related
So I'm trying to insert some text into a already existing table. If I insert the text into the text field and press submit, I'll get a var_dump on a other page that shows the information that I want to insert but it doesnt send it to the database or something. I hope you can help me with what I'm doing wrong.
This is my insert query
public function insertComment($commentaar, $idBestelling){
try{
$stmt = $this->_db->prepare('UPDATE `apotheek`.`Bestelling` SET `commentaar` = :commentaar WHERE `Bestelling`.`idBestelling` = :idBestelling;');
if($stmt->execute(array(
':commentaar' => $commentaar,
':idBestelling' => $idBestelling))){
return true;
} else {
return false;
}
} catch(PDOException $e) {
echo '<p class="bg-danger">'.$e->getMessage().'</p>';
}
}
And here you can find the code from the webpage.
<div class="row">
<form role="form" method="post" action="/?content=bezorgen">
<div class="col-xs-12 col-sm-12 col-md-12 loginborder">
<h2 class="loginhead">Bestelling no. <?php print($data['idBestelling']); ?> <?php ($data['isSpoed'] == '0') ? print('') : print('deze bestelling is met SPOED') ?></h2>
<hr>
<div class="row col-6 col-md-6 col-sm-12 col-xs-12">
<div class="loginhead">
<h3>Commentaar</h3>
<hr>
</div>
<div>
<textarea rows="9" cols="74" name="commentaar"><?php print($data['commentaar']); ?></textarea>
</div>
</div>
<div class="row registerbtn">
<div class="col-xs-12 col-md-12" style="text-align:right;"><input type="submit" name="submit" value="Verstuur" class="btn btn-lg" tabindex="5"></div>
</div>
</div>
</form>
And the php
$data = $user->getBestellingPatient($_POST['idBestelling']);
if(isset($_POST['submit'])){
$user->insertComment($_POST['commentaar'], $_POST['idBestelling']);
};
I think you are trying to update a comment which is already exist in the database.
You should add a hidden field in the form to hold the id of the the post (Mostly auto incremented id from DB)
Add the below element below your textarea element
<input type='hidden' name='idBestelling'<?php print($data['idBestelling']); ?>/>
So when you are updating id will be passed as a part of the form then you can receive it usinig $_POST
you should try this:
$stmt = $this->_db->query('UPDATE `apotheek`.`Bestelling` SET `commentaar` = :commentaar WHERE `Bestelling`.`idBestelling` = :idBestelling;');
I am working with a fairly straight forward form yet for some reason it is not submitting NULL results and instead submits them as empty results.
<div class="span6">
<form action="" method="POST">
<div class="block-fluid without-head">
<div class="toolbar clearfix">
<div class="right">
<div class="btn-group">
<button type="submit" class="btn btn-small btn-warning tip" data-original-title="Submit Aritcle">
<span class="icon-ok icon-white"></span>
</button>
<button type="button" class="btn btn-small btn-danger tip" data-original-title="Delete Article">
<span class="icon-remove icon-white"></span>
</button>
</div>
</div>
<div class="left">
<div style="font-size: 11pt; font-family: -webkit-body; font-weight: bolder;">Article Exchange Request</div>
</div>
</div>
<div class="row-form clearfix">
<div class="span3">Title</div>
<div class="span9"><input type="text" name="title" value="" placeholder="main subject title"></div>
</div>
<div class="row-form clearfix">
<div class="span3">First Sentence</div>
<div class="span9"><input type="text" name="s1" value="" placeholder="Sentence with max of 200 characters"></div>
</div>
<div class="row-form clearfix">
<div class="span3">Second Sentence</div>
<div class="span9"><input type="text" name="s2" value="" placeholder="Optional sentence with max of 200 characters"></div>
</div>
<div class="row-form clearfix">
<div class="span3">Third Sentence</div>
<div class="span9"><input type="text" name="s3" value="" placeholder="Optional Sentence with max of 200 characters"></div>
</div>
<div class="row-form clearfix">
<div class="span3">Website Url</div>
<div class="span9"><input type="text" name="link" value="" placeholder="Url to be included in articles"></div>
</div>
<div class="row-form clearfix">
<div class="span3">Website Url Title</div>
<div class="span9"><input type="text" name="link_text" value="" placeholder="Url text to be included in articles limit 150 characters"></div>
</div>
<div class="row-form clearfix">
<div class="span3">Credit Offer</div>
<div class="span9"><input type="text" name="cost_value" value="" placeholder="Example: 100"></div>
</div>
</div>
</form>
</div>
The problem is someone can just come along and submit the form without entering any data at all, and it still submits, and adds empty results to every column in the database. Meaning no NULL values are being set in the database.
My form processing
<?
if (isset($_POST[cost_value])) {
$stmt = $db->prepare("SELECT credits from member_credits WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$row = $stmt->fetch();
$count = $row[credits];
if ($count > $_POST[cost_value]) {
$stmt = $db->prepare('INSERT INTO article_requests (title, s1, s2, s3, link, link_text, offer, username) VALUES (:title, :s1, :s2, :s3, :link, :link_text, :offer, :username)');
$stmt->bindValue(':title', $_POST[title]);
$stmt->bindValue(':s1', $_POST[s1]);
$stmt->bindValue(':s2', $_POST[s2]);
$stmt->bindValue(':s3', $_POST[s3]);
$stmt->bindValue(':link', $_POST[link]);
$stmt->bindValue(':link_text', $_POST[link_text]);
$stmt->bindValue(':offer', $_POST[cost_value]);
$stmt->bindValue(':username', $username);
$stmt->execute();
}
}
?>
What I expect to happen on submission is if no value is entered in the form it should be set to NULL... that would mean if no $_POST[cost_value] was set, the form wouldn't submit. As it stands now, it is ALWAYS seeing $_POST[cost_value] as being set - sometimes to a value such as $_POST[cost_value] = '100'; and other times $_POST[cost_value] = ''; but in both cases, the form will submit. Equally I have the database set so that only the values of s2 and s3 should be able to be null, however when everything is submitting empty instead of null, thats mostly useless.
What exactly am I missing? Why will it not say $_POST[cost_value] = NULL; if the user has not input a value? Why is it submitting ''?? I have a feeling its going to be something basic I am overlooking, I just don't see it....
UPDATE FOR CLARITY
I know there are a number of ways to fix this. I can use if then tests, I can use temporary variables, I can check for the value itself is something other than ''. I know WHAT the work arounds are. I don't understand WHY they are needed. Why is it I make forms every day of the week using if isset, and everyday that works. If a form has no values input from the user, the form won't submit because an if isset returns false. So why is it on this particular form it is submitting '' instead of saying !isset don't process.
// Assign a var to the post.
$DIRTY_Var = $_POST['varinputname'];
// ensure the var is cleaned (no funny business when submitting)
$CLEAN_Var = filter_var($DIRTY_Var,` FILTER_SANITIZE_STRING);
// check if the var is empty, or has value. If empty it will set the var to null;
if (!$CLEAN_Var) { return $CLEAN_Var = null; }
// validate input
$valid = true;
// insert data
if ($valid) {
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO Table ("
."ColumnNames"
.")"
."VALUES("
."?"
.")";
$q = $pdo->prepare($sql);
$q->execute(array($CLEAN_Var));
// Once INSERT is completed, we will redirect.
header("Location:" 'link.php'); exit;
}
Try
If(isset($_POST['cost_value']) && !empty($_POST['cost_value']){
// your code
}
I am working on a code segment that is a messaging function next to a list of names. On the page there is an envelope and when you click it a pop-up window appears with a text area inside of it. I would like to be able to fill that textarea with characters and send it as a message using my already functioning sendMessage function. I know my sendMessage function works and I've isolated the issue to the return given by the submit button. How do I get my submit button to POST the textarea to the page (keep in mind there are 2+ forms on this page). The code looks like this:
if (empty($_POST) === false) {
print_r($_POST);
$blah = $_POST;
echo "<script>window.top.location='asd.$blah'</script>";
}
if (empty($_POST['Send']) === false) {
echo "<script>window.top.location='../hidden/PNMasdd.php?gpa=$var1&year=$var2&major=$var3&sport=$var4'</script>";
echo 'YOU ARE IN THE SENDING MESSAGE FIELD';
if( empty ($_POST['message_full']))
{
$errors[] = 'All fields must be filled in!';
//print_r($errors);
}
if(empty($_POST) === false && empty($errors) === true)
{
$receiver_id = $temp_user['id'];
$sender_id = $user_data['id'];
$type = 0;
if(empty($is_Convo))
{
$is_Convo = 0;
}
$blahVar = $user_data['id'] . ',';
$message = str_replace("\r\n", "<br>", $_POST['message_full']);
$message_data = array(
'sender_id' => $user_data['id'],
'receiver_id' => $receiver_id,
'message_full' => $message,
'is_opened' => $blahVar,
'isConvo' => $is_Convo,
'type' => $type
);
//Put data limit code here
send_message($message_data);
$my_exten = "Messages/messages.php";
$noti_data = array(
'id_receive' => $receiver_id,
'id_sent' => $user_data['id'],
'type' => 1,
'exten' => $my_exten
);
notify($noti_data);
echo '<center>';
echo '<div class="alert alert-info">
<strong>It sent!</strong> You have sent a message successfully!
</div>
';
echo '</center>';
}
else{
echo output_errors($errors);
}
}
?>
<form id="wow" name="wow" action="" method="POST">
<div class="row">
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<center>
<textarea maxlength="100000" data-msg-required="Please enter your message." rows="10" cols="100%" class="form-control" name="message_full" id="message_full"></textarea>
</center>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<table width=90%><td width=45%><button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button></td><td width=45%>
<button type="submit" value="Send Message" name="Send" onclick="document.getElementById('wow').submit()" class="btn btn-primary">Send</button>
As pointed out in the comments, the buttons need to be inside your form.
Your setup is a bit odd for my taste, but try this out.. it will mostlikely need some tweaks and i'm not sure about the cancel button..
<form id="wow" name="wow" action="" method="post" >
<div class="form-group required">
<div class="col-md-12">
<textarea name="message_full" id="message_full" rows="10" cols="100%" class="form-control" maxlength="10000" placeholder="Please enter your message." autocomplete="off"></textarea>
</div>
</div>
<div class="buttons"> <!-- add css for this or change the class to yours.. -->
<input class="btn btn-default" data-dismiss="modal" value="Cancel" />
<input class="btn btn-primary" type="submit" value="Send Message" />
</div>
</form>
As #Barmar just said, you don't have any field named 'Send'. You should change:
if (empty($_POST['Send']) === false)
to:
if (empty($_POST['message_full']) === false)
(Edit)
Only the fields inside your form will get to $_POST superglobal, the submit button will not get there
$sql = "SELECT * FROM item";
$stmt = $conn->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$data['result_2'] .= '
<div class="col-sm-4 col-md-4">
<div class="content-boxes style-two top-column clearfix animated flipInY" style="opacity: 1;">
<div class="content-boxes-text">
<form action="php/additem.php" method="post" class="form-inline pull-right">
<h4>'.$row['itemName'].'</h4><input type="hidden" name="itemName" value="'.$row['itemName'].'">
<img src="../wholesale/img/sourdough.jpg" class="img-reponsive">
<p>'.$row['description'].'</p><input type="hidden" name="description" value="'.$row['description'].'">
<div class="form-group">
<label class="sr-only" for="exampleInputAmount">Qty</label>
<div class="input-group">
<input type="number" name="qty" class="form-control" id="exampleInputAmount" placeholder="How Many?">
</div>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
<!-- //.content-boxes-text -->
</div>
<!-- //.content-boxes -->
</div>
';
}
I want to be able to add an if statement in this result_2 string.
This is for displaying a product, and i would like to display price depending on users session value?
eg.
if ($_SESSION['customer_x'] == a) {
display price a
}
else if ($_SESSION['customer_x'] == b) {
display price b
}
Is this the correct method to be able to add an if statement to a JSON query?
After Starting your while loop, put a if else there,
$price ="";
if ($__SESSION['customer_x'] == a) {
$price='display price a';
}
else if ($_SESSION['customer_x'] == b) {
$price='display price b';
}
and now echo this price where ever you want to in your html
this is more neet and less messy way
You can use a ternary if operator to have conditional statements inside strings, example
$bool = false;
echo "The value of \$bool is " . ($bool == true ? "TRUE" : "FALSE");
Example in use
To add that you can keep doing the same but:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ // the next of the content
$priceOfSession = ($__SESSION['customer_x'] == a) ? 'pricea' : 'priceb';
$data['result_2'] .= '
<div class="col-sm-4 col-md-4">
'.$priceOfSession.'
</div>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
<!-- //.content-boxes-text -->
</div>
</div>
';
}
So if you want to evaluate only two conditions if otherwise the if you want, simply add it there. Do as you add the $ row but before defining value.
I have been searching through various questions on the site with no luck. The closest I got to an answer, as far as I can tell, was through the following question: CodeIgniter's form action is not working properly.
The problem is that when I press the login button nothing happens so the action property in the HTML form is not being triggered. I have looked at my routes as well with no luck.
The HTML code is in my view folder and is the following:
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-default">
<div class="panel-body">
<form role="form" method="post" action="<?php echo base_url() ?>index.php/login">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Username" name="username" type="username" required>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password" required>
</div>
<a type="submit" id="login-button" style="navy" class="btn btn-lg btn-success btn-block">Login</a>
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
My controller is called pages.php, and it's function, login is the following:
public function login()
{
$this->load->helper('form');
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($username != false && $password != false)
{
$loginDetails = $this->pages_model->retrieveLoginDetails($username, $password);
if ($loginDetails != false)
{
if ($loginDetails['username'] == $username && $loginDetails['password'] == $password)
{
$this->load->view ('home');
}
else
{
$this->view();
echo "details dont match";
}
}
else
{
$this->view();
echo "nothing received from db";
}
}
else
{
$this->view();
echo "no details entered";
}
}
I am only concerned with going to the home page. By calling the view function I am just returning to the login screen. I will adjust the naming appropriately once I can get this to work. The pages model contains the following function to retrieve the login data from the database:
public function retrieveLoginDetails ($username, $password)
{
$loginDetails['username'] = $this->db->get_where('user_details', $username);
if (! empty ($loginDetails['username']))
{
$loginDetails['password'] = $this->db->get_where('user_details', $password);
return $loginDetails;
}
return false;
}
Any help would be greatly appreciated. Thanks.
Use
<input type="submit" value="Login" class="btn btn-lg btn-success btn-block" />
In place of
<a type="submit" id="login-button" style="navy" class="btn btn-lg btn-success btn-block">Login</a>
To know more check out this : http://www.w3schools.com/tags/tag_a.asp
I don't know direct anwser, but did you check what exactly is your server returning and where are data beeing sent. LiteBug FTW ;)
If there isn't any data beeing sent, you should probally check your .js files.