I'm trying to create a form where you can type in information and choose several options. Based on what is chosen i would like to take id and create record in database.
<form method = "post" action = "http://localhost:81/servisai/knygynas/insert_book.php">
<input type = "submit" value = "Pridėti knygą">
<tbody>
<?php while($row = $resultb->fetch_assoc()) : ?>
<tr>
<td style="display:none"><input type = "text" name = "Autoriaus_id"></td>
<td><input type = "text" name = "Autoriaus_vardas" class="form-control" value = "<?php echo $row['Vardas']; ?>"></td>
<td><input type = "text" name = "Autoriaus_pavarde" class="form-control" value = "<?php echo $row['Pavarde']; ?>"></td>
<td><input type = "checkbox" name = "myCheckbox" ></td>
</tr>
<?php endwhile ?>
</tbody>
</table>
</form>
I'm not sure how to choose which records to send to insert_book.php upon submitting
I think it's supposed to do something with if (isset($_POST['myCheckbox'])) but dont know where to go from here
You can do it by following
<input type = "hidden" name = "Autoriaus_id[]" value="<?php echo $row['id']?>">
foreach($_POST['Autoriaus_id'] as $id){
//do your stuffs here
}
Related
I currently Have this code for inserting into my receipt table,
$query = "insert into receipt(petname,receipttype)
values('$petname','$receipttype')";
in which i obtain the autogenerated id
$receiptid=mysqli_insert_id($db);
and send it to the next page. however when the record is inserted, another is also generated (Two of the same content but another auto-generated ID, is entered into the database)
http://prntscr.com/kpjff6 [example of duplicate fields with unique ids]
if (isset($_POST['cmdedit'])){
$petname = mysqli_real_escape_string($db,$_POST['petname']);
$receipttype = mysqli_real_escape_string($db,$_POST['receipttype']);
$query = "insert into receipt(petname,receipttype)
values('$petname','$receipttype')";
$result = mysqli_query($db,$query);
if (mysqli_query($db,$query)){
$receiptid=mysqli_insert_id($db);
if ($result) {
echo "<font color = 'green' > Receipt sucessfully obtained! Page will auto-redirect to order confirmation page in 5 seconds! </font>";
header( "refresh:5; url=addorderhomepage.php?animalid=".$animalid."&receiptid=".$receiptid);
}else{
echo "<p>Something went wrong! </p>" . mysqli_error($db);
}
}
}
?>
<div class = "topbar">
<h2> Order Receipt </h2>
</div>
<?php
$query = "select *
from catalogue
where animalid= " . $animalid;
$result = mysqli_query($db,$query);
if ($result){
while ($rows = mysqli_fetch_array($result))
{
?>
<form method = "post" action = "" >
<table>
<tr>
<th> Animal ID</th>
<td bgcolor="#FFFFFF"> <input type ="text" name = "txtanimalid" value = "<?php echo $rows[0]; ?>" readonly /> </td>
</tr>
<tr>
<th> Animal Name </th>
<td> <input type ="text" name = "petname" value = "<?php echo $rows[1]; ?>" readonly /> </td>
</tr>
<tr>
<th> Animal Type </th>
<td> <input type ="text" name = "petname" value = "<?php echo $rows[3]; ?>" readonly /> </td>
</tr>
<tr>
<th> Animal Species </th>
<td> <input type ="text" name = "petname" value = "<?php echo $rows[2]; ?>" readonly /> </td>
</tr>
<tr>
<th> Animal Description </th>
<td> <input type ="text" name = "petname" value = "<?php echo $rows[4]; ?>" readonly /> </td>
</tr>
<tr>
<th> Receipt type </th>
<td> <input type ="text" name = "receipttype" value = "printable" readonly /> </td>
</tr>
<tr>
<th> Receipt Date </th>
<td> <input type ="date" name = "orderdate" value="<?php echo date('Y-m-j'); ?>" readonly="readonly" </td>
</tr>
<tr>
<th> <br/><br/> </th>
<td> </td>
</tr>
<tr>
<th> </th>
<td> <input type ="submit" value = "Obtain receipt" name = "cmdedit" /> </td>
</tr>
The duplicate row are probably happen because you are calling 2 time the mysqli_query with the same data, the first time when storing the response in the $result var and the second time inside the condition in the if statement.
// ... code
$result = mysqli_query($db,$query);
- if (mysqli_query($db,$query)) {
+ if ($result) {
$receiptid = mysqli_insert_id($db);
if ($result) {
// .. rest of the code
I have searched for this and keep getting errors no matter what I try.
The main error I get is:
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, string given
I have created a page showing all records. The user then selects one record to edit and the id is passed on to this page which would show all the fields in a form/table. a few of the fields are readonly so can not be modified.
What I am trying to achieve is to show the results in a table so that they can be edited and then sent to the database and updated. I was thinking of sending it to another file to update and then send them back to view the results page.
<form action = 'modify2.php' method = 'post'>
<table border="1">
<tr style="border-bottom:1px solid #000000;">
<strong>User ID</strong>
<strong>Username</strong>
<strong>Password</strong>
<strong>Email</strong>
<strong>Name</strong>
<strong>Submitted By</strong>
<strong>Memeber Since</strong>
</tr>
<?PHP
//error check to see what results are obtained
echo '<br />sql code = '.$sql.'<br/>'; // this works bot the $result
shows nothing
//die();
//fetch object array and put into a row
//while($row = mysqli_fetch_assoc($result))
while($row = mysqli_fetch_assoc($sql))
{
?>
<tr><td><label>User ID :</label></td>
<td><input type = 'text' name = 'id' id = 'id' value='<?PHP echo $row["id"];?>' readonly/></input><br /></td></tr>
<tr><td><strong>Username</strong></td>
<td><input type = 'text' name = 'username' id = 'username' value='<?PHP echo $row["username"];?>'/></input><br /></td></tr>
<tr><td><label>password :</label></td>
<td><input type = 'text' name = 'password' id = 'password' value='<?PHP echo $row["password"];?>'/></input><br /></td></tr>
<tr><td><label>email :</label></td>
<td><input type = 'text' name = 'email' id = 'email' value='<?PHP echo $row["email"];?>'/></input><br /></td></tr>
<tr><td><label>Real Name :</label></td>
<td><input type = 'text' name = 'Name' id = 'Name' value='<?PHP echo $row["Name"];?>'/></input><br /></td></tr>
<tr><td><label>submitted by :</label></td>
<td><input type = 'text' name = 'submittedby' id = 'submittedby' value='<?PHP echo $row["submittedby"];?>' readonly/></input><br /></td></tr>
<tr><td><label>trn_date :</label></td>
<td><input type = 'text' name = 'trn_date' id = 'trn_date' value='<?PHP echo $row["trn_date"];?>' readonly/></input></td></tr>
<?PHP
}
//close table here
?>
<tr><td colspan='2'><div align='center'><input type = 'submit' value ='Modify Record' name = 'submit'/></td></tr>
</form></div>
</table>
You need to put $result=mysqli_query($con,$sql); first to you can use
// Associative array
$row=mysqli_fetch_assoc($result);
pls i'm trying to insert variable values from a php file into this html file using ajax but i don't know how to start here is the form
<form>
<table>
<tr>
<td>FULL NAME</td>
<td><input type = 'text' name = 'fullname' id = 'fullname'></td>
<td>CUID</td>
<td><input type = 'text' name = 'idno' ></td>
</tr>
<tr>
<td>SURNAME</td>
<td><input type = 'text' name = 'sname' id = 'sname'></td>
<td>TITLE</td>
<td>
<select name='title'>
<option>Select Title</option>
<?php
$conn = db();
$stmt=$conn->prepare('select TITLE from titles order by TITLE');
$stmt->execute();
while($data = $stmt->fetch()){
echo "<option>".$data['TITLE']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>FIRST NAME</td>
<td><input type = 'text' name = 'fname' id = 'fname'></td>
</tr>
<tr>
<td>MIDDLE NAME</td>
<td><input type = 'text' name = 'mname' id = 'mname'></td>
</tr>
<tr>
<td>DESIGNATION</td>
<td>
<select name='post' id ='post'>
<option>Select Post</option>
<?php
$conn = db();
$stmt=$conn->prepare('select post from posts order by post');
$stmt->execute();
while($data = $stmt->fetch()){
echo "<option>".$data['post']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>GENDER</td>
<td>
<select name='gender'>
<option>Select Gender</option>
<?php
$conn = db();
$stmt=$conn->prepare('select SEX from sx order by SEX');
$stmt->execute();
while($data = $stmt->fetch()){
echo "<option>".$data['SEX']."</option>";
}
?>
</select>
</td>
<td>DATE OF RESUMPTION</td>
<td><input type = 'date' name = 'dresume' id = 'dresume'></td>
<td>CATEGORY</td>
<td>
<select onchange='bn(this.value);' name='category'>
<option>Select Category</option>
<?php
$conn = db();
$stmt=$conn->prepare('select CATEGORY from categories order by CATEGORY');
$stmt->execute();
while($data = $stmt->fetch()){
echo "<option>".$data['CATEGORY']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>DEPARTMENT</td>
<td>
<select name='unit'>
<option>Select Department</option>
<?php
$conn = db();
$stmt=$conn->prepare('select DEPARTMENT from units order by DEPARTMENT');
$stmt->execute();
while($data = $stmt->fetch()){
echo "<option>".$data['DEPARTMENT']."</option>";
}
?>
</select>
</td>
<td>UNIT</td>
<td><input type = 'text' name = 'unit' id = 'unit'></td>
<td>KOL</td>
<td><input type = 'text' name = 'kol' id = 'kol'></td>
</tr>
<tr>
<td>LEVEL</td>
<td>
<select name='level' >
<option>Select Level</option>
<?php
$conn = db();
$stmt=$conn->prepare('select level from levels');
$stmt->execute();
print_r ($stmt);
while($data = $stmt->fetch()){
echo "<option>".$data['level']."</option>";
}
?>
</select>
</td>
<td>STEP</td>
<td>
<select name='step'>
<option>Select Step</option>
<?php
$conn = db();
$stmt=$conn->prepare('select step from steps');
$stmt->execute();
print_r ($stmt);
while($data = $stmt->fetch()){
echo "<option>".$data['step']."</option>";
}
?>
</select>
</td>
<td>SALARY SCALE</td>
<td><input type = 'text' name = 'sscale' id = 'sscale'></td>
</tr>
<tr>
<td>RESPONSIBLITY/DUTY POSY</td>
<td><input type = 'text' id = 'dutypost' name = 'dutypost'></td>
<td>RESPONSIBLITY ALOWANCE</td>
<td><input type = 'text' id = 'rallowance' name = 'rallowance'></td>
</tr>
<tr>
<td>SPECIAL PAYMENT</td>
<td><input type = 'text' id = 'spayment' name = 'spayment'></td>
<td>PAY PERCENTAGE</td>
<td><input type = 'text' id = 'ppercent' name = 'ppercent'></td>
</tr>
<tr>
<td>HOUSE DEDUCTION</td>
<td><input type = 'text' id = 'hded' name = 'hded'></td>
<td>RENT DEDUCTION</td>
<td><input type = 'text' id = 'rded' name = 'rded'></td>
<td>FURNITURE DEDUCTION</td>
<td><input type = 'text' id = 'fded' name = 'fded'></td>
</tr>
<tr>
<td>NHF</td>
<td><input type = 'text' id = 'nhf' name = 'nhf'></td>
<td>NHF_NO</td>
<td><input type = 'text' id = 'nhfno' name = 'nhfno'></td>
</tr>
<tr>
<td>PFA</td>
<td><input type = 'text' id = 'pfa' name = 'pfa'></td>
<td>PFA_NO</td>
<td><input type = 'text' id = 'pfano' name = 'pfano'></td>
</tr>
</table>
</form>
when an edit button is clicked these varibale will automatically appear in the form pls i'll need a head start at this still new at it thanks
here is the php file
if(isset($_POST['edit'])) {
$dresume = $data['DRESUME'];
$kol = $data['KOL'];
$level = $data['LEVEL'];
$step = $data['STEP'];
$dpost = $data['DutyPost'];
$dpay = $data['DutyPay'];
$spay = $data['Special'];
$ppercent = $data['Paypercent'];
$rentd = $data['HDEDP'];
$house = $data['HOUSE'];
$furnided = $data['FURNIDED'];
$nhf = $data['NHF'];
$nhfno = $data['NHF_NO'];
$pfa = $data['PFA'];
$pfano = $data['PFA_NO'];
}
The basic thing you should understand is that PHP executes only in server and you cannot execute your php script in html once the page is loaded.
Now you have a doubt that how did I get php written inside html worked?
Answer your file got parsed in the server and all php script written in between the "" executed and corresponding html is generated, See the source of your html in browser.
Once the page is served to browser your php code will not execute. To do any modification in the html loaded in browser, either you need to reload the page or you need some client script that executes in browser. Javascript can be used as a client script.
Ajax is a method of communication between your html loaded in browser and your server. ie, if you want to send some data to or from your html loaded in browser, you can make use of ajax. https://webdesignerhut.com/pass-data-with-ajax-to-a-php-file/ is a good example
You can find good and better documentations before you start
Basically, a form HTML send parameters to an other PHP page.
If you want do it in ajax, you need to send the form anyway
or put the ajax on the form page
form.html
<form method="post" action="success.php">
<input type="text" name="id" />
</form>
success.php
<?php echo $_POST['id']; ?>
That's the ajax way (on the same page)
form.html
<form method="post" id="formAjax" action="success.php">
<input type="text" id="inputId" name="id" />
</form>
<script>
$('#formAjax')on('submit', function() {
var id = $('#inputId').val()
alert('input id:'+id);
}
</script
When updating my table, it works fine. but the only problem is that i can't seem to echo the fields in the input.
and showing the error, undefined index. i don't get it, i tried everything. but it won't echo.
For instance:
Undefined index: fam_name in /Applications/XAMPP/xamppfiles/htdocs/finalproject/admin/records/activities_update.php on line 80
i have no problem in getting the ID echoed. but the other variables fam_name , fam_add, nochild, aid and so on... i can't retrieve it. i hope you understand what i'm trying to say
i tried using $_POST , $_GET and rows, doesn;t work =\
need help been trying to solve this for days.
<?php
if(isset($_GET['success']) === true && empty($_GET['success']) === true){
echo ' Your details have been updated';
} else {
if (empty($_POST) === false && empty($errors) === true) {
$fam_data = array(
'fam_name' => $_POST['fam_name'],
'fam_add' => $_POST['fam_add'],
'nochild' => $_POST['nochild'],
'aid' => $_POST['aid'],
'date_visited' => $_POST['date_visited'],
'visited_since' => $_POST['visited_since'],
);
edit_family($fam_data);
header('Location: rec_act1.php?success');
exit();
//exit();
} else if (empty($errors) === false) {
echo output_errors($errors);
}
?>
<div id="insert_form">
<form name="update" action = "" method ="post">
<table border = "0" align = "center">
<tr>
<th colspan = "2"> </th>
</tr>
<tr>
<td><input type = "hidden" name = "id" maxlength = "9" value="<?php echo $id = $_GET['id'];?>"></td> Editing: <?php echo $id = $_GET['id'];?>
</tr>
<tr>
<td>Family Name: </td>
<td><input type = "text" name = "fam_name" maxlength = "9" value="<?php echo $_POST['fam_name']; ?>"></td>
</tr>
<tr>
<td>No. Of Family: </td>
<td><input type = "text" name = "nochild" maxlength = "30" value="<?php echo $row['nochild'];?>"></td>
</tr>
<tr>
<td>Family Address: </td>
<td><input type = "text" name = "fam_add" maxlength = "30" value="<?php echo $row['fam_add'];?>"></td>
</tr>
<tr>
<td>Types of Aid received: </td>
<td><input type = "float" name = "aid" step = 'any' min = 1 max = 99999999 value="<?php echo $row['aid'];?>"></td>
</tr>
<tr>
<td>Date Visited</td>
<td><input type = "date" name = "date_visited" value="<?php echo $row['date_visited'];?>"></td>
</tr>
<tr>
<td>Visited Since </td>
<td><input type = "date" name = "visited_since" value="<?php echo $row['visited_since'];?>"></td>
</tr>
<td><input type ="submit" name = "submit" value = "Update"></td></table>
</form></div><? } ?>
$row used in form doesn't exists.
Before form you need to select current data from table, than saved that data to $row.
$row = $mysqli->fetch_assoc($mysqli->query($conn, "SELECT ...")); // select family data
In $mysqli is MySQLi instance, of course.
Due to updating row I expect you have data in your database. If you don't know, and don't want to see undefined index notices, in form values has to be an isset, eg.
value="<? echo isset($row['fam_name']) ? $row['fam_name'] : ''; ?>"
Guessing here as it's not clear where you're using echo, but I am guessing the data is lost after the header redirect as the information most certainly won't be available in rec_act1.php. Try rewriting the script to not redirect, or if you must, pass along the data in a session.
For get data in $_GET you need to send all of your variables in query string like
if (empty($_POST) === false && empty($errors) === true) {
$fam_name = $_POST['fam_name'];
// set your all vars
<------rest of code ---->
header('Location: rec_act1.php?id=$id&fam_name=$fam_name');
and get your query string var in value like:-
value="<?php echo $_GET['id'];?>"
value="<?php echo $_GET['fam_name'];?>"
// and so on
Do not echo $_POST variables directly, it allows for the possibility of XSS attacks, always sanitize first. I also notice in your code, that you are creating the $fam_data array and directly afterward redirecting without creating a $_SESSION so the array is going to be lost unless your edit_family function starts a session. The issue to me, seems to be that you are not receiving your $_POST['fam_name'] variable from the form or it is lost on page refresh.
Try adding...
//start session before any output to browser
session_start();
if (isset($_POST['fam_name'])) {
//only process the request if expected values are found
if (ctype_alpha($_POST['fam_name'])) {
$_SESSION['fam_name'] = $_POST['fam_name'];
}
} else {
//this alert will let you know if the value was empty
echo "no family name received";
exit;
}
Then in your form input, use...
value="<?php echo $_SESSION['fam_name']; ?>">
HI I NEED YOUR HELP..
I have this VIEW which show all items according to its company
<?php foreach($item_list as $item2):?>
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="<?php echo $item2->item_id ?>" ></td>
<td><?php echo $item2->item_desc?></td>
<td><?php echo $item2->serial_num ?></td>
<td><input type = "checkbox" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "checkbox" name = "JobStatus[]" value = "job order" ></td>
</tr>
<?php endforeach; ?>
---------------------------------------- html output ---------------------------------------
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="146" ></td>
<td>sample item 01</td>
<td>123</td>
<td><input type = "radio" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "radio" name = "JobStatus[]" value = "job order" ></td>
</tr>
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="147" ></td>
<td>sample item 02</td>
<td>21344</td>
<td><input type = "radio" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "radio" name = "JobStatus[]" value = "job order" ></td>
</tr>
My problem is that I want to insert this to new value (status, item_id, item_desc, serial_num, JobStatus) to quotation table in mysql. If Click I on radio item_id 146 row it will insert all values under item_id 146 likewise to item_id 147 it will insert all item values under 147 item_id
So you want the radio button to be checked according to it's job status?????
If so, try something along these lines ( i don't know how your your database is set up so you will have to alter the following):
<?php foreach($query as $item):?>
<td><input type = "text" name = "item_id" value ="<?php echo $item->item_id ?>" ></td>
<td><?php echo $item->item_desc?></td>
<td><?php echo $item->serial_num ?></td>
<td><input type="radio" name="JobStatus" value="quotation" <?php $item->job_status == 'quotation' ? echo "checked" : null; ?> ></td>
<td><input type="radio" name="JobStatus" value="job order" <?php $item->job_status == 'job order' ? echo "checked" : null; ?> ></td>
</tr>
<?php endforeach; ?>
Enclose the whole loop inside a form tag with action="index.php/controller/method"
In your controller,
public function method() {
$items = $this->input->post('item_id', TRUE);
$job_status = $this->input->post('JobStatus', TRUE);
$stati = $this->input->post('status', TRUE);
foreach($items as $key => $row) {
$this->your_model->insert_new($row, $job_status[$key], $stati[$key]);
}
}
In your_model, write the database query to insert these data wherever you need. Of course you need to change the names - method, controller and your_model to suit your application.