$_POST is not working on second page - php

- FIXED -
When i submit form, the row i want to edit in table is forcing 0 instead of setting 0 or 1 (when i switch). It means that table (row) is somehow getting value 0.
This is my form:
<form class="form-horizontal" action="<?php echo BASE_URL?>process.php?task=edit_notify" method="post">
<div class="form-group">
<div class="material-switch pull-left">
<p><span class="control-label" style="color: rgba(75, 75, 75, 0.6); cursor: default;"><?php echo ADMIN_NOTIFY; ?></span></p>
<input id="allow_reg_msg" name="allow_reg_msg" type="checkbox" value="1" <?php if($row['allow_reg_msg'] == 1) echo "checked='checked'"; ?> />
<label for="allow_reg_msg" class="label-primary"></label>
</div>
</div></br>
<div class="form-group">
<button type="submit" class="btn_"><i class="fa fa-pencil"></i> <?php echo EDIT; ?></button>
</div>
</form>
When i submit, it should send value to process.php page, here is the "process.php?task=edit_notify":
if(isset($_GET['task']) && $_GET['task'] == "edit_notify"){
if(!$user->isLoggedIn()) Redirect::to('index');
if(!$user->hasPermission('admin')) Redirect::to('index');
$allow_reg_msg = $_POST['allow_reg_msg'];
$sql = "UPDATE `settings` SET
`allow_reg_msg` = :allow_reg_msg
WHERE `id` = 1";
$query = $handler->prepare($sql);
$query->execute(array(
":allow_reg_msg" => $allow_reg_msg
));
Session::flash('edit_site_success', SUCCESS_UPDATE);
Redirect::to('admin/index.php?page=edit_site');
}
And here are few images from my database:
image 1,
image 2

I found solution! It was in my define('BASE_URL', 'https://monkstudio.biz/');
Because i have in .htaccess my website link with "www", here in script it was not set, now i put define('BASE_URL', 'https://www.monkstudio.biz/'); and it's working fine!
I'm so proud i found this by myself.

Related

How to insert data to database table from $_GET value on PHP

Why I can't insert into 1 field to my database table?
I loop the data of my other table to display each data and put the result to href tag so it clickable and concatenate with it's id so it will put the id result after the link like this:
<a href="scheduling.php?CID=<?php echo $rows['docID']; ?>">
Here is how I loop my data using php on page1:
<?php
$sel = "SELECT * FROM doctors ORDER BY docID";
$result = $conn->query($sel);
if($result->num_rows>0)
{
while($rows = $result->fetch_array())
{
?>
<a href="scheduling.php?CID=<?php echo $rows['docID']; ?>">
<div class="doc_item" style="width:310px; border:4px solid #009973;display:inline-block; margin-top:40px; margin-right:20px;">
<img src="images/ft-img.png" style="width: 300px;">
<div class="item-lvl" style="width: 100%; background:#009973; color:#fff; height:70px; padding-top:10px;">
<h4 style="font-size:20px; font-weight:bold;">Dr. <?php echo $rows['docFname']; ?></h4>
<span><?php echo $rows['docType']; ?></span>
</div>
<div style="width: 100%; background:#00664d;padding-top:15px; padding-bottom:20px;">
<h3 style="color:#fff;">Make a Schedule!</h3>
</div>
</div>
</a>
<?php
}
}
?>
Then I want to add the value of CID to my database table using this code on page 2:
<?php
if (isset($_POST['btn-submit'])) {
$cusID = $customerID;
$docID = $_GET['CID'];
$checkupType = $_POST['checkupType'];
$schedTime = $_POST['schedTime'];
$contact = $_POST['contact'];
$ins = "INSERT INTO schedule
(customerID, docID, checkupType, schedTime, contact)
VALUE ('$cusID','$docID','$checkupType','$schedTime','$contact')";
if($conn->query($ins)===TRUE) {
header('location:success_sched.php');
}else{
echo "SQL Query: " . $ins . "->Error: " . $conn->error;
}
}
Now, this code insert data to my database table except $docID which is the value is $_GET['CID'];
docID column is intiger.
please correct my code, this is my school project.
Form code on page 2:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<div class="sign-u">
<div class="sign-up1">
<h4>Form of Checkup* :</h4>
</div>
<div class="sign-up2">
<label>
<input type="radio" value="Virtual" name="checkupType" required>
Virtual Checkup
</label>
<label>
<input type="radio" value="Facetoface" name="checkupType" required>
Face to Face Checkup
</label>
</div>
<div class="clearfix"> </div>
</div>
<p style="color:gray;">*Take note that virtual checkup will require a you stable internet for better communication.</p>
<div class="sign-u">
<div class="sign-up1">
<h4>Date and Time* :</h4>
</div>
<div class="sign-up2">
<input type="date" name="schedTime" id="date" required>
</div>
<div class="clearfix"> </div>
</div>
<p style="color:gray;">*Please provide Skype id as a contact information if <span style="color:#ff5c33">Virtual Checkup</span> is your choice.</p>
<div class="sign-u">
<div class="sign-up1">
<h4>Contact Information :</h4>
</div>
<div class="sign-up2">
<input type="text" name="contact">
</div>
<div class="clearfix"> </div>
</div>
<div class="sub_home" style="text-align: right;">
<input style="background: #339966;" type="submit" name="btn-submit" value="Submit">
<div class="clearfix"> </div>
</div>
</form>
Please understand that when you submit a form, that is a new request to the server. It has a new URL. Your page 2 form submits to $_SERVER['PHP_SELF']; - which is the current URL but without the querystring variables. Therefore your GET parameter is not passed in the form submit. (If you use the "View Source" feature of your browser to examine the "action" attribute of the form after it's rendered, you'll see exactly what URL it makes the request to when submitting. You can also see the same thing by looking at your browser's Network tool, or your webserver's access logs.)
There are a couple of ways around that, but I'd suggest adding the value as a hidden field in the form, and populating it with the value of the GET parameter during the previous request.
e.g.
<input type="hidden" value="<?php echo $_GET["CID]; ?>" name="CID">
and then in the PHP, retrieve it via $_POST like all the other variables:
$docID = $_POST['CID'];
Finally, please ensure you update your code to use prepared statements and parameters, as recommended in the comments, so that you are protected against both SQL injection attacks and unexpected syntax errors. You can get more info and examples here

GET elements in the url are missing after after submitting in POST format

here is my code
if(isset($_GET['id']) && $_GET['id']!=''){
$id= get_safe_value($con, $_GET['id']);
$res=mysqli_query($con, "select * from `categories` where id='$id'");
$row=mysqli_fetch_assoc($res);
$categories=$row['categories'];
}
if(isset($_POST['submit'])){
$categories= get_safe_value($con, $_POST['categories']);
if(isset($_GET['id']) && $_GET['id']!=''){
mysqli_query($con, "UPDATE `categories` SET `categories`='$categories' WHERE `categories`.`id`=$id");
}else{
mysqli_query($con,"INSERT INTO `categories` ( `categories`,`status` ) VALUES ('$categories', '1')");
}
echo "<script>window.location.href='categories.php'</script>";
die();
}
to edit my categories, i sent id to the url like this
echo "<a href='manage-categories.php?id=".$row['id']."'>Edit </a>";
but after form submission, that second if block unable to catch the id. Any solution?
this is my form
<form method="POST" action="manage-categories.php">
<div class="card">
<div class="card-header">
<h4 class="card-title">Insert Categories</h4>
</div>
<div class="card-block">
<div class="card-body">
<h5 class="mt-2">Category Name</h5>
<fieldset class="form-group">
<input type="text" class="form-control" id="placeholderInput" value="<?php echo $categories; ?>" name="categories" placeholder="Enter category name">
</fieldset>
<div class="form-group">
<input class="btn btn-primary btn-min-width mr-1 mb-1" name="submit" type="submit" value="Insert">
</div>
<div class="field_error">
<?php
echo "<p>";
echo $msg;
echo "</p>";
?></div>
</div>
</div>
</div>
</div>
This form is used for both updation and insert
GET and POST values don't persist between requests. When you submit a form, you're making a new request to a new URL.
To ensure the value is sent to the server again when submitting the form, you can either:
put the querystring (GET) parameter into the URL you send to when submitting the form e.g. by injecting it into the form's "action" attribute.
Or
create a hidden field in your form which contains the relevant ID, and then make your PHP code search for $_POST["id"] instead of the GET version.
The action in the form most likley does not have the Query String defined.
<form method="post" action="manage-categories.php?id=<?php echo $row['id']; ?>">
...
Make sure you truely post with the query string.

Even when DB has records, while trying to edit records, code does not work

I am trying to update records in 'pantry-info' table. Code goes to the if loop instead of else
The code is mentioned below, what am I doing wrong?
DB connection is:
require 'config/connectDB.php';
session_start();
$id = $_GET['id'];
$sql = 'SELECT * FROM temp WHERE'. " pan_id = '$id'";
$result = mysqli_query($conn,$sql);
Trying to read DB table values in form as follows:
<?php
if(mysqli_num_rows($result1) == 0)
{
echo '<h2>This record already exists. Do you want to delete it?</h2>';
} else { ?>
<h4>Edit the record here:</h4>
<br>
<form name="pantryinfo" id="pantryForm" method = "post" action="update.php" data-toggle="validator" role="form">
<?php while ($row = mysqli_fetch_array($result1,MYSQLI_ASSOC)) { ?>
<div class="control-group form-group">
<div class="controls">
<input type="hidden" class="form-control" id="panid" name="panid" value="<?php echo $row['pan_id'];?>">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo $row['pname'];?>" required>
<p class="help-block"></p>
</div>
</div>
</form>
<?php
}
}
?>
Similar code works fine on a different page retrieving values from another DB table. Please help. Thanks in advance.
I resolved the issue. In the SQL query I used $result as a variable and in the if-else code I was using result1.
Thank you Fred for pointing it out.

Why does my entire code disappear if I remove my form tag?

NOTE: Here is the fiddle, and since that looks awful, Here is the full screen result
Here is the fiddle after I do my changes on my computer and generate the html, and here is the full screen
Note on the fiddle:
I added the entire code into the fiddle since it won't work unless it's the whole code.
Thanks to anyone who will give hints! My only problem here is the one in the title, nothing else. :)
=====
I'm working on an undergraduate project to create an interface that accesses a database (though I'm not a CS student). My code is behaving very strangely. I have code for one window that displays one large div encasing three divs side byside, which is structured this way:
<div id="container">
<div id="window1">
<form></form> //my buttons
</div>
<div id="window2">
<form></form> //my body
</div>
<div id="window3">
//I havent written anything here yet
</div>
</div>
But I realized it makes more sense for me to encase them all in just one form. Like this:
<div id="container">
<form>
<div id="tab1">
</div>
<div id="tab2">
</div>
<div id="tab3">
</div>
</form>
</div>
So in the first case, this is what my website looks like:
But after I change it:
What's weird is, if I put forms in my forms, it starts working again! So this works:
<div id="container">
<form>
<div id="tab1">
<form></form>
</div>
<div id="tab2">
<form></form>
</div>
<div id="tab3">
</div>
</form>
</div>
If you need to see my code, I actually am using PHP to generate the html since it's a lot, so (this is the method that doesn't work):
<div id="search_separator" class="upper_tab">
<form action="" method="post">
<div id="button_container">
<?php
$tbs_a = 1;
$tbs_name = 'table_selector';
$printRetain_button = '';
$fg = input::get($tbs_name);
echo '<label for="',$tbs_name,'"></label>';
foreach($table_arrays AS $ta => $table){
$tbs_val = "table".$tbs_a;
if($tbs_a==1){
echo '<div class="button"><input type="radio" class="tabl_selector" name="',$tbs_name,'" value="',$tbs_val,'" checked="default"/><div class="table_selector">',$ta,'</div></div>';
}else{
echo '<div class="button"><input type="radio" class="tabl_selector" name="',$tbs_name,'" value="',$tbs_val,'"';
if($tbs_val == $fg){
echo ' checked="checked"';
}//make a separate echo for table1 radio button with the default turned on.
echo '/><div class="table_selector">',$ta,'</div></div>';
}
$tbs_a++;
}
?>
<!--<input type="submit" name="submit" value="submit" />-->
</div>
<div id="search_container" class="content">
<?php
$a = 1;
$search = DB::getInstance();
foreach($table_arrays AS $t_a => $table){
$y = 1;
echo '<div id="table',$a,'_content" class="table_content">';
echo "<h2>",$t_a,'</h2><table align="center" id="actualhtmltable">';
foreach($table AS $t => $field){
$name = "table".$a."_field".$y;
$val_sticky = escape(input::get($name));
$val_find = $search->get($t_a, array($t, '=', input::get($name)));
//$val_find = $search->get('user_credentials', array('user_id', '=', 28))->results();
if(!empty($val_find)){
$val = array();
foreach($val_find[0] AS $vfz){
$val[] = $vfz;
}
} elseif(!empty($val_sticky)){
$valu = $val_sticky;
} else {
$valu = "";
}
echo '<tr><div><td>',$t,'</td><td><label for="',$name,'"><input type="text" class="entryfields" name="',$name,'" value="';
if(!empty($val[$y-1])){
echo $val[$y-1];
} else {
echo $valu;
}
echo '" /></label></td></div></tr>';
$y++;
}
echo '</table></div>';
$a++;
}
?>
<div class="Tableformbutton">
<div class="FindModeButtons">
<input type="Submit" name="find" value="Find" id="find" class="findupdateinsert" />
<input type="Submit" name="update" value="Update" id="update" class="findupdateinsert" />
</div>
<input type="Submit" name="insert" value="Insert" id="insert" class="findupdateinsert" />
</div>
</div>
<div id="other_container">
<!--find mode on and other functionalities-->
</div>
<div class="hassubmit" id="searchsubmit_container">
<input type="button" id="findmodetoggler" name="query_submit" value="Toggle Find Mode" class="top_buttons" />
<a href="#" class="tip"><input="button" name="Help" value="Help" class="top_buttons" id="help" />
<span> Click "Toggle Find Mode" to toggle between find/update and insert buttons.<br />
If find mode is on, you will be able to look for existing records and update them. Otherwise, you can only insert new entries.<br />
Find mode is off by default.</span>
<p>Need Help?</p>
</a>
</div>
</form>
</div>
As far as I can tell, it is supposed to work! I don't see anything in my HTML, and as for my CSS, I have styled nothing about forms in there, only divs and labels and input tags. What could be wrong?
Thanks to anyone who'll give hints or ideas!
I found the solution, which really is just so simple. But, I don't know why it happened this way.
I added an id="search_form" into the form I need, then used CSS to say .search_form{ height: 100% } as apparently, when the form was made, it made the height of everything into just a few pixels. This is weird, but at least I found the answer.

insert form to database

i have program codeigniter anda i want to insert form to database.
code view:
<form target="paypal" method="post">
<div class="field1">
<div class="field">
<label>Nama</label>
<input placeholder="Nama" name="nama" type="text">
</div>
<div class="field">
<label>No. HP</label>
<input placeholder="No. HP" name="handphone" type="text">
</div>
<div class="field">
<label>Alamat</label>
<input placeholder="alamat" name="alamat" type="text">
</div>
<div class="field">
<label>Jumlah</label>
<div class="selectbox">
<select name="jumlah" id="">
<?php for ($i=1; $i <= 20; $i++): ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>
</div>
</div>
<button type="submit" name="submit" class="ui teal button order-button">Order now</button>
</div>
</form>
code controller
function simpanOrder()
{
$this->load->model("M_order");
$data['nama'] = $_POST['nama'];
$data['handphone'] = $_POST['handphone'];
$data['alamat'] = $_POST['alamat'];
$data['jumlah'] = $_POST['jumlah'];
if($this->input->post('submit')){
$this->M_order->insert($data);
}
}
when i click submit data not insert to database. so can you help me with this code problem? thanks.
Your form doesn't have an action, and therefore may not be going to the function you want it to. (/controller/function)
<form target="paypal" method="post">
Also, instead of using a button to submit the form - try using <input type="submit"...
Using the <button>, in some browsers, you would have "submit" submitted, in others, "Order now".
If the above doesn't work - check your SQL.
As a side note, CodeIgniter has a form helper and a form_validation library which are quite useful if you're already using CodeIgniter. That won't fix your problem but it's just something I felt I would point out.
See:
http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html
http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
Call model from controller. and write below code in model.
$data = array(
'handphone' => $this->input->post('handphone'),
'alamat' => $this->input->post('alamat'),
)
In this array key is database columnname.
$this->db->insert(yourtablname, $data);
$insert_id = $this->db->insert_id();
You need to define action attribute in form tag where you will provide controller name and method name like this
<form action="<?php echo site_url('controllername/simpanOrder')?>" method="post">
After posting you can debug your code like this
$post = $this->input->post();
echo '<pre>';
print_r($post);
Then
if($this->input->post('submit')){
$this->M_order->insert($data);
}
And finally
echo $this->db->last_query();
This will display you the last query run.

Categories