So basically, I've got a function in PHP that deletes an user from the same row when a form is submit, the form does submit, the function does do it's thing, but the user is not deleted.
$html = '';
$html .= '<table><form method="post" action="index.php?controller=user&action=delUser">';
foreach( $auser as $user) {
$html .= '<tr><td><input type="hidden" value="'.$user['id'].'"><input type="submit" name="delUserSubmit" value=""> '.$user["id"].' '.$user["voornaam"].' '.$user["tv"].' '.$user["achternaam"].'</td></tr>';
}
$html .= '</form></table>';
return $html;
}
So in this form, it sends the id through a hidden input field which carries the user id
public function delUser()
{
if(isset($_POST['delUserSubmit'])) {
$sql = "DELETE FROM user WHERE id = ".$_POST['id'];
$this->oDb->insUpdDelQuery($sql);
unset($_POST);
header("Location: index.php?controller=user&action=show");
}
}
When it reaches the above function, it goes through the isset $_POST etc. Since when everytime I do submit the form, I go to the page at which the header is pointing.
But when it reaches the user page, the user is still there.
Now the problem could either be:
I don't actually send the id
I do send the id, but the $sql doesn't get the id somehow? the delUser() function is empty whereas something like show($id=null) shows all the users.
the delUser() needs something like $_POST['id'], but that would give unexpected characters in my editor.
Can't fix this, anybody able to help?
POST values are submitted using the name attribute on the input field.
Change your HTML input to
<input type="hidden" name="id" value="'.$user['id'].'">
You have not set anything in the from to $_POST['id']. The name of the posted element is taken from the name in the html form. The input tag that has the value of id needs to have the the name set to id:
<input type="hidden" value="'.$user['id'].'" name="id">
Related
I am working with multiple forms on a page, where pressing a button will edit that specific post (using the hidden input variable).
I am dealing with two issues here:
When I do press edit, it will grab the last hash from the list (I am using a foreach loop to iterate through the list).
When I do press edit and redirect to the next page, the $_POST variable is deemed null.
Page 1:
foreach ($result as $item) {
echo '
<form method="post" action="editPost">
<input type="hidden" id="messageID" value="' . $messageID . '">
# Print $item iterations here in the form of a html form
<div>
<input type="submit" value="Edit" name="editPost">
</div>
</form>
';
}
Page 2:
if ($_POST['messageID'] == null) {
echo '<script>alert("Key error")</script>';
} else {
# Do things if $_POST['messageID'] is not null
}
You should not add Form inside the loop and input or form with same id or name in loop will confuse html to what should send and may it will send last one,
instead of form in loop just add link of other page with edit record id or hash as in query string
example.com/pagetwo.php?editid=$messageid
and from page 2 you can get that variable useing
$_GET['editid'] or $_REQUEST['editid']
and based on id you need to get data from database and fill in inputs to update it
is there any sugestion to store default value that is posted to database from form html?
my aplication is using mysql as the rdmbs and codeigniter for backend process.
right now, i just using html method at the submit form.
<input type="hidden" id="" name="" value="">
at the html page and set the value there.
but the value is visible when you open the source code in any other browser.
is there any method to store value that is not visible from user in codeigniter?
If your form is posting the data on the same method wherefrom it is rendered then the best approach is that; keep the value in the method itself and use it after form submit.
public function edit() {
$id = 12;
if ($this->form_validation->run()) {
$dataComm = array(
'id' => $id
'name' => $this->input->post('name')
);
$result_comm = $this->Common_model->insert_data('cw_franchise_commission', $dataComm);
}
$this->data['title'] = "Edit User";
$this->load->view('edit', $this->data);
}
And if your form action is another method then you can encode the value and decode it after form submission.
To Encode
bin2hex(base64_encode($id))
To Decode
base64_decode(hex2bin($this->input->post('id')));
One more thing if you are using hidden inputs then you should name it something confusing not clear at all. Otherwise, anyone can understand the purpose of the field.
There is no such a way but you can keep it in encoding format using
base64_encode()
<input type="hidden" name="id" value="<php echo base64_encode('1231231231'.$id); ?>">
"1231231231" just for additional security which will preprend with the id
cut "1231231231" in controller like below to get the id
function abc() {
if (isset($_POST['id']) && !empty($_POST['id'])) {
$id = substr(base64_decode($_GET['id']), 10); // to remive extra string
if (!empty($id)) {
// you get id here
} else {
//if someone attempt to tamper with the id then it will retrive blank
}
} else {
//redirect or show error
}
}
Good morning everyone,
I have a page that performs a default connection to database, search and looped output of data to the page by applying a template for each result in the table using this code:
<?php
$result = mysqli_query($con,"SELECT * FROM XXX WHERE Type='ABCD';");
while($row = mysqli_fetch_array($result))
{
include('includes/template-1234.php');
}
mysqli_close($con);
?>
On the same page I have a simple HTML search box:
<div id="srch-form">
<form>
<input name="srch-box" type="text" id="srch-box" size="45" onClick="this.value='';" onFocus="this.select()" onBlur="this.value=!this.value?'Enter Product Name or SKU#':this.value;" value="Enter Product Name or SKU#">
<input type="submit" name="srch-btn" id="srch-btn" value="Search">
</form>
</div>
Because I am just learning PHP - how do I set this up so that the default action still occurs on page entry, but - onsubmit of something in the searchbox the data in the current page is replaced by the results of the submission? I guess I'm getting confused over the fact that since I have already assigned a value(s) to $result to get the default output, If the value of $result is changed by the search form, does PHP automatically refresh the page when the value of $result is changed? - Sorry if this is a simple thing.
First, you'll have to add an action (which file to refer when submitted) and preferably a method-type in your form-tag. You will also have to include value of $productName (given from server-side php) inside of your html (look at value attribute)
Do something like this:
html-form: (Examplefile: yourhtmlform.php)
Then in your php-file get value from a specific element like this:
<?php
//File querydb.php (example filename)
//Form is submitted and srch-box is set (only set when form is submitted)
if(isset($_POST['srch-box'])) {
$type = $_POST['srch-box']; //Get value of input-element
}
else {
$type = 'kingkong'; //Default type to search
}
$prepareQuery = mysqli_prepare($dbConnectionLink,"SELECT * FROM XXX WHERE Type=:type");
//...code to fetch results..
//You get a productName (or SKU) from db
if (isset($result['product_name'])) {
$productName = $result['product_name']; //or whatever your column is called
}
else {
$productName = '';
}
include('yourhtmlform.php'); //Observe that it must be php to echo out value of $productName in this included file
I noticed you didn't use prepared-statement. Look for more information on prepared statements here: http://www.php.net/manual/en/mysqli.prepare.php (use them!)
I hope this will help you somehow :-)
I’m usually Using input type submit eg: <input type=“submit” name=“assign” value=“Assign”/>
and using this is no problem for me but now I want to use button eg:<button type=“button” class=“button” id=“deleteb”><div>Assign Student</div></button>
but don’t know how to used it or call it to my controller.
this is my controller function
if($assign_student)//<input type="submit" name="assign" value="Assign"/>
{
if($maxMember->max_members > $countMember)
{
if($countMember+1 == 1)
{
$is_leader = 1;
}
else
{
$is_leader = $this->input->post('is_leader');
}
$student = array(
'user_id' => $this->input->post('student'),
'group_no' => $this->input->post('group'),
'is_leader' => $is_leader
);
$this->admin_db->save_group($student['group_no'],$student);
}
else
{
$data['max_error'] = "<p class='error'>Sorry, This group reached the maximum number of members!</p>";
}
}
If you want the button to submit the form you must have type="submit"
If you want the button to send a value, it's better to use a hidden input to send along additional information. Example:
<input type="hidden" name="assign" value="Assign" />
You can set a name and value to the <button>, but guess what?: In IE6, the actual html content of the button will be sent as the post data instead. It's one of my favorite bugs.
It's not very clear why you posted your controller code, but if you are checking for a "trigger" value before processing, like $this->input->post('assign'), you can check for the presence of any other of the form values instead, or the presence of any $_POST values, or as I mentioned: a hidden input.
If you press a submit-button, you are posting a form to some
script, in this case PHP.
if you submit the form, your browser sends the information
contained in the form to the receiving script.
eg, if you make a page with a form that contains:
You can check like this...
if (isset($_POST["deleteb"]))
{
//Do stuffs
}
I've got the following php code printing out the contents of a SQL table.
$query="select * from TABLE";
$rt=mysql_query($query);
echo mysql_error();
mysql_close();
?>
<i>Name, Message, Type, Lat, Lng, File </i><br/><br/>
<?php
while($nt=mysql_fetch_array($rt)){
if($nt[name] != null){
echo "$nt[id] $nt[name] $nt[message] $nt[type] $nt[lat] $nt[lng] $nt[file]";
}
}
?>
How would I implement a button so for each "row" if the button is clicked on that row it'll submit the information of that row to another php file?
I want it looking something like...
details details2 details3 BUTTON
details4 details5 details6 BUTTON
details7 details8 details9 BUTTON
details10 details11 details12 BUTTON
Where if BUTTON was hit on row 1 details1,2,3 would be sent to a php file, on row 2 detals 4,5,6 would be sent etc. How would I do this?
Thanks.
it's going to be something like that, depending on the data you need to send:
while($nt = mysql_fetch_array($rt)) {
if($nt[name] != null){
echo "$nt[id] $nt[name] $nt[message] $nt[type] $nt[lat] $nt[lng] $nt[file] ".'send request<br/>';
}
}
You can either use GET method and send a query string to the second php page and receive the variables there, like
next.php?variable1=value1&variable2=value2&...
or use POST method by making a hidden form for each row and assign a hidden field for each variable you want to send.
<form method="post" action"next.php">
<input type="hidden" name="variable1" value="value1" />
<input type="hidden" name="variable2" value="value2" />
.
.
.
</form>
or instead of sending all the values, just send the row ID (if any) using any of these two methods and run another query in next.php to get the information you need from database.
Instead of submitting the entire data, just send the ID and fetch the results from the database in the other script. If you want to have an input button, you can do
<form action="/other-script.php" method="GET">
<?php printf('<input type="submit" name="id" value="%s" />', $nt["id"]); ?>
</form>
but you could also just add a link, e.g.
printf('Submit ID', $nt["id"]);
If you really want to send the entire row values over again, you have to make them into form inputs. In that case, I'd send them via POST though.