Php script when i click on button or image - php

I have this code `Lessons List
$sqlessons = 'SELECT * FROM lessons ';
$lessons = $mysqli->query($sqlessons);
while ($row=$lessons->fetch_assoc())
{
echo "<li>$row[lesson_name] : <span>$row[lesson_description]</span></li>";?>
<form action="script.php" method="get">
<input type="submit" value="Add this lesson now!">
</form>
<?php } ?>
</p>
</ul>`
this code presents a list of lessons with a button below of every lesson.
I want to add to this button a funtion that adds to my database :
$user_id ( i have that loaded in session )
$lesson_id (i have that only inside the 'while')
so if I click the button on Lesson1 (lesson_id =1) and I am the user Nick D (user_id=2) then the script will add a line to my database table usersVSlessons :
10,1,2 (the 10 is a random foreign key of the table).
I think you get what I want to do :P
Any ideas?

Place both user_id & lesson_id in input type hidden, get values onclick submit buttom & then make an ajax call to store data in db.
(you can either generate random no. in javascript or you can also use php to generate random value & input type to hidden place it)

Related

Retrieve a database record using a value in url without $_POST

Dear whom that may be able to assist.
Link - https://develop.hangoutsdubai.ae/category-beach-entertainment.php
Currently I am using $_POST method for my listings to be able to retrieve the record in the database as a single page listing.
On the above link page, I will add the following to preview :
I am using below $dashedURL to remove %20 in URL and replace with '-'
//############# REPLACE URL %20 WITH DASH (-) #############//
$string = 'category-beach-entertainment-single-profile.php?'.$rs_BeachEntertainmentVenuesListing_rows['nvenueName'].'';
str_replace('%20', '-', $string);
$dashedURL = str_replace(' ', '-', rawurldecode($string));
//############# PART OF FORM BEING SUBMITTED #############//
<!-- CATEGORY BUTTON -->
<form method="post" action="'.$dashedURL.'">
<div>
<input type="submit" value="'.$rs_BeachEntertainmentVenuesListing_rows['nmainCategory'].'" class="singleProfileDetailsCategory">
<input type="hidden" name="txtnid" value="'.$rs_BeachEntertainmentVenuesListing_rows['nid'].'">
</div>
</form>
<!-- H3 VENUE TITLE -->
<form method="post" action="'.$dashedURL.'">
<div>
<span class="name" style="display:none;">'.$venueName.'</span>
<input type="submit" value="'.$rs_BeachEntertainmentVenuesListing_rows['nvenueName'].'" class="name singleProfileDetailsVenueName" itemprop="name">
<input type="hidden" name="txtnid" value="'.$rs_BeachEntertainmentVenuesListing_rows['nid'].'">
</div>
</form>
//############# ON SINGLE PAGE #############//
$txtnid = $_POST['txtnid'];
$sql_BeachEntertainmentVenuesListing = "SELECT * FROM tblvenueListingsBeachEntertainment WHERE nid = $txtnid";
//CONNECT TO MYSQL SERVER
require('inc-conn.php');
//EXECUTE SQL STATEMENT
$rs_BeachEntertainmentVenuesListing = mysqli_query($vconn, $sql_BeachEntertainmentVenuesListing);
//CREATE AN ASSOCIATIVE ARRAY
$rs_BeachEntertainmentVenuesListing_rows = mysqli_fetch_assoc($rs_BeachEntertainmentVenuesListing);<br><br>
Currently my issue is when I copy the single page URL of the venue and would like to share it with someone, the record will display blank. So if I don't select the venue through the main listing page : https://develop.hangoutsdubai.ae/category-beach-entertainment.php it won't display because of $_POST.
For example : Copy this link of a venue and open in a new window or cognitive window and it will display a blank record : https://develop.hangoutsdubai.ae/category-beach-entertainment-single-profile.php?Venue-9-Name
Is it possible to use a different method in order to retrieve all the data from a record without using the main page with all the listings?
The reason is for in case some one would want to share the link of a specific venue but it will show no data. I have tried couple of things but getting stuck. According to my hypothesis there should be a variable that retrieves the name of the venue in the url and retrieve the unique id from that record, then display the data accordingly. Sort of struggling to figure this one out. Any help will be gladly appreciated.

Can't get form to update mysql record

I have a three part form that works like this, the first form is called create_ticket.php and it's basically a form with a customers contact info and what they want done. When the ticket is filled out it get's submitted to MySQL then when a search is done of the db you can select the ticket you want by clicking on the select link associated with that ticket in the search result and the browser will open a new window with the 2nd part of the form but it will pass the id associated with that MySQL record in the browser so you see at the end of the url ?id=10 now the 2nd part of the form is what a technician will use to fill out what they did and when they click get signature on the bottom of the page it will pop up the 3rd part of the form which allows the customer to sign on the tech's ipad or Toughbook and when they click accept it should update that record in MySQL associated with that id passed in the url. My issue is it's not working. so you can view the form at http://jemtechnv.com/test/ticket_results.php and see when you click on select it brings you to the technicians form to be filled out.
Here is my code that processes the 2nd part of the form:
<?php
// database connection //
include 'db/db_connect.php';
//This gets all the other information from the form
// start of form inputs //
$work_performed=$_POST['work_performed'];
$item_qty1=$_POST['item_qty1'];
$item_qty2=($_POST['item_qty2']);
$item_qty3=$_POST['item_qty1'];
$item_qty4=($_POST['item_qty2']);
$item_qty5=$_POST['item_qty1'];
$manuf_1=$_POST['manuf_1'];
$manuf_2=$_POST['manuf_2'];
$manuf_3=$_POST['manuf_3'];
$manuf_4=$_POST['manuf_4'];
$manuf_5=$_POST['manuf_5'];
$part_number1=$_POST['part_number1'];
$part_number2=$_POST['part_number2'];
$part_number3=$_POST['part_number3'];
$part_number4=$_POST['part_number4'];
$part_number5=$_POST['part_number5'];
$part_description1=$_POST['part_description1'];
$part_description2=$_POST['part_description2'];
$part_description3=$_POST['part_description3'];
$part_description4=$_POST['part_description4'];
$part_description5=$_POST['part_description5'];
$part1_price=$_POST['part_price1'];
$part2_price=$_POST['part_price2'];
$part3_price=$_POST['part_price3'];
$part4_price=$_POST['part_price4'];
$part5_price=$_POST['part_price5'];
$price_extension1=$_POST['price_extension1'];
$price_extension2=$_POST['price_extension2'];
$price_extension3=$_POST['price_extension3'];
$price_extension4=$_POST['price_extension4'];
$price_extension5=$_POST['price_extension5'];
$material_total=$_POST['material_total'];
$sales_tax=$_POST['sales_tax'];
$shipping_cost=$_POST['shipping_cost'];
$work_date1=$_POST['work_date1'];
$work_date2=$_POST['work_date2'];
$work_date3=$_POST['work_date3'];
$work_date4=$_POST['work_date4'];
$work_date5=$_POST['work_date5'];
$tech_name1=$_POST['tech_name1'];
$tech_name2=$_POST['tech_name2'];
$tech_name3=$_POST['tech_name3'];
$tech_name4=$_POST['tech_name4'];
$tech_name5=$_POST['tech_name5'];
$cost_code1=$_POST['cost_code1'];
$cost_code2=$_POST['cost_code2'];
$cost_code3=$_POST['cost_code3'];
$cost_code4=$_POST['cost_code4'];
$cost_code5=$_POST['cost_code5'];
$pay_rate1=$_POST['pay_rate1'];
$pay_rate2=$_POST['pay_rate2'];
$pay_rate3=$_POST['pay_rate3'];
$pay_rate4=$_POST['pay_rate4'];
$pay_rate5=$_POST['pay_rate5'];
$total_hours1=$_POST['total_hours1'];
$total_hours2=$_POST['total_hours2'];
$total_hours3=$_POST['total_hours3'];
$total_hours4=$_POST['total_hours4'];
$total_hours5=$_POST['total_hours5'];
$hours_subtotal1=$_POST['hours_subtotal1'];
$hours_subtotal2=$_POST['hours_subtotal2'];
$hours_subtotal3=$_POST['hours_subtotal3'];
$hours_subtotal4=$_POST['hours_subtotal4'];
$hours_subtotal5=$_POST['hours_subtotal5'];
$total_hours=$_POST['total_hours'];
$material_total=$_POST['material_total'];
$labor_cost=$_POST['labor_cost'];
$grand_total=$_POST['grand_total'];
//Writes the information to the database
mysql_query("UPDATE INTO tickets WHERE id=$id (work_performed, item_qty1, item_qty2, item_qty3, item_qty4, item_qty5,manuf_1, manuf_2, manuf_3, manuf_4, manuf_5, part_number1, part_number2, part_number3, part_number4, part_number5, part_description1, part_description2, part_description3, part_description_4, part_description_5, part1_price, part2_price, part3_price, part4_price, part5_price, price_extension1, price_extension2, price_extension3, price_extension4, price_extension5, material_total, sales_tax, shipping_cost, work_date1, work_date2, work_date3, work_date4, work_date5, tech_name1, tech_name2, tech_name3, tech_name4, tech_name5, cost_code1, cost_code2, cost_code3, cost_code4, cost_code5, pay_rate1, pay_rate2, pay_rate3, pay_rate4, pay_rate5, total_hours1, total_hours2, total_hours3, total_hours4, total_hours5, hours_subtotal1, hours_subtotal2, hours_subtotal3, hours_subtotal4, hours_subtotal5, total_hours, material_total, labor_cost, grand_total,)
VALUES ('$work_performed','$item_qty1','$item_qty2','$item_qty3','$item_qty4','$item_qty5','$mauf_1','$manuf_2','$manuf_3','$manuf_4','$manuf_5','$part_number1','$part_number2','$part_number3','$part_number4','$part_number5','$part_description1','$part_description2','$part_description3','$part_description_4',
'$part_description5','$part1_price','$part2_price','$part3_price','$part4_price','$part5_price','$price_extension1','$price_extension2','$price_extension3','$price_extension4','$price_extension5','$material_total','$sales_tax','$shipping_cost','$work_date1','$work_date2','$work_date3','$work_date4','$work_date5','$tech_name1','$tech_name2','$tech_name3','$tech_name4','$tech_name5','$cost_code1','$cost_code2','$cost_code3','$cost_code4','$cost_code5','$pay_rate1','$pay_rate2','$pay_rate3','$pay_rate4','$pay_rate5',
'$total_hours1','$total_hours2','$total_hours3','$total_hours4','$total_hours5','$hours_subtotal1','$hours_subtotal2','$hours_subtotal3','$hours_subtotal4','$hours_subtotal5','$total_hours','$material_total','$labor_cost','$grand_total',)") ;
mysql_affected_rows();
?>
<html>
<body>
<center>
<br><br><br>
<form name="results" method="post" action="ticket_results.php" enctype="multipart/form-data" id="ticketresult">
<input type="submit" class="submit" id="ticketresult" style="width: 165px" value="Do Something">
</form>
</center>
</body>
</html>
I'm also aware I need to take MySQL injection precautions but honestly at the moment that's not a priority as I just want to get the form working first then I will go back and clean up the code. Thanks for your help! I've disabled the signature portion of the form so clicking on get signature on the 2nd form will process the form and do the updates for that record, well it's supposed to but doesn't!
UPDATE: I have changed the query syntax as suggested below to this:
mysql_query("UPDATE tickets SET (work_performed, item_qty1, item_qty2, item_qty3, item_qty4, item_qty5,manuf_1, manuf_2, manuf_3, manuf_4, manuf_5, part_number1, part_number2, part_number3, part_number4, part_number5, part_description1, part_description2, part_description3, part_description_4, part_description_5, part1_price, part2_price, part3_price, part4_price, part5_price, price_extension1, price_extension2, price_extension3, price_extension4, price_extension5, material_total, sales_tax, shipping_cost, work_date1, work_date2, work_date3, work_date4, work_date5, tech_name1, tech_name2, tech_name3, tech_name4, tech_name5, cost_code1, cost_code2, cost_code3, cost_code4, cost_code5, pay_rate1, pay_rate2, pay_rate3, pay_rate4, pay_rate5, total_hours1, total_hours2, total_hours3, total_hours4, total_hours5, hours_subtotal1, hours_subtotal2, hours_subtotal3, hours_subtotal4, hours_subtotal5, total_hours, material_total, labor_cost, grand_total,)WHERE id=$id
VALUES ('$work_performed','$item_qty1','$item_qty2','$item_qty3','$item_qty4','$item_qty5','$mauf_1','$manuf_2','$manuf_3','$manuf_4','$manuf_5','$part_number1','$part_number2','$part_number3','$part_number4','$part_number5','$part_description1','$part_description2','$part_description3','$part_description_4',
'$part_description5','$part1_price','$part2_price','$part3_price','$part4_price','$part5_price','$price_extension1','$price_extension2','$price_extension3','$price_extension4','$price_extension5','$material_total','$sales_tax','$shipping_cost','$work_date1','$work_date2','$work_date3','$work_date4','$work_date5','$tech_name1','$tech_name2','$tech_name3','$tech_name4','$tech_name5','$cost_code1','$cost_code2','$cost_code3','$cost_code4','$cost_code5','$pay_rate1','$pay_rate2','$pay_rate3','$pay_rate4','$pay_rate5',
'$total_hours1','$total_hours2','$total_hours3','$total_hours4','$total_hours5','$hours_subtotal1','$hours_subtotal2','$hours_subtotal3','$hours_subtotal4','$hours_subtotal5','$total_hours','$material_total','$labor_cost','$grand_total',)");
but now I get the following error when I submit the form to update the database,
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(work_performed, item_qty1, item_qty2, item_qty3, item_qty4, item_qty5,manuf_1, ' at line 1
Your query is wrong. The query should be
UPDATE tickets SET work_performed = something, item_qty1 = something .... WHERE id=$id

Simple Chechlist PHP Application Help needed

I am building a simple PHP checklist application which takes sentences as input which are stored in MYSQL database then the list is echoed out with remove buttons in front of each list item such as:
1) Do breakfast [remove button]
2) Go to College [remove button]
3) Eat Lunch [remove button]
4) Get back Home [remove button]
Now what would be function that will be executed to remove the current list item being removed.
Here is how I am retrieving the list from database and their corresponding remove buttons:
$sql2 = "SELECT * FROM `info` LIMIT 0, 30 ";
$arr = mysql_query($sql2);
while($data = mysql_fetch_array($arr)){
echo '<strong>'.$data['id'].')</strong>'.' '.$data['text'].'<form action="list.php" method="POST">
<input type="submit" value="Remove" />
</form>'.'<br />';
Please help me how can we approach to a situation like this.
P.S The database has two columns only "ID" and the "List item"
You can use < input type='hidden' name='data' value="<?php echo $data[text]; ?>" > in the form
Finally, you can put this block to remove.
if(isset($_POST['submit']) {
$query = "delete from info where text = '$_POST[data]'";
//run the query here
}
What is it that you want particularly?
Do you mean you want the application to get check box elements and store them in the database.
Then get that data and apply to each a delete command?
be specific my friend and we can help.

PHP Mysql & Jquery dynamically populating multiple records

I want to above Master and child system by using PHP,MYSQL & JQuery.
I am attaching sample image link below See screenshot
Product Quantity and UOM is field which belong to MAster Table and
Code, Component, category, quantity (Also) & UOM (duplicate) is belong to Child table.
I want to add Code, Component, category, quantity etc multiple time whenever user click on add.
Just need to know how can i save all these multiple records when someone completed their works and click on Final Save Button?
I am really and very aggressively searching for this but didn't get any anwer.
If anyone who can find the way or any help or anything that will help me towards this system.
Thanks a lots pls pls Help
you'll want to use
jQuery ajax to save data
.clone() to add a record in the UI you'll have to reset the values will your at it
that should get you started
Each time your user clicks 'add' you want to take the values of your form inputs, build a new table row and show their selected values. This is easy enough, but you also need to add hidden inputs which represent what they chose in the select boxes above, so when the user clicks save, the whole form is posted and you can process the input. A simple example would be:
<script>
var count = 0;
$('#add').click(function(event)
{
var code = $('#code').val(),
component = $('#component').val()
category = $('#category').val(),
uom = $('#uom').val();
$('#table').append(
'<tr>'
+ '<td>' + code + '<input type="hidden" name="record[' + count + '][code]"></td>'
+ '<td>' + component + '<input type="hidden" name="record[' + count + '][component]"></td>'
+ '<td>' + category + '<input type="hidden" name="record[' + count + '][category]"></td>'
+ '<td>' + uom + '<input type="hidden" name="record[' + count + '][uom]"></td>'
+ '</tr>'
);
/*
EDIT: I changed this to a DECREMENTOR so our keys don't overlap and override
anything that is CURRENTLY in the database
*/
count --;
})
</script>
This would attach a click handler to the add button. Each time it is clicked, we get the values of the inputs, store them in a variable, and build + append a new table row to your "preview table" below, which shows the values they selected and creates hidden inputs which can be processed later after the user clicks Save.
Some notes about this:
- it only gets the value of the selected inputs (so for the select boxes, the value of the option not the text. you'll have to do some extra work to replace that into your table row.
- your entire table will have to be encapsulated in a <form> tag, which your save button must also be inside.
Once you get the posted data to the server, do a print_r($_POST) to see what it looks like, you should be able to figure out how to process it fairly easily.
edit
Okay, so you asked a lot of questions here, i'll try to address them as best I can, without writing a novel.
What if someone mistakenly clicks on add and wants to cancel the addition (or changes their mind, whatever).
This actually isn't that hard. If this happens, just remove the appended table row from your table using $.remove. Since all the hidden input elements are contained within the table row, they will also be removed from the form so when the user posts, the fields will not be present.
How should you sanitize the data?
Sanitize the data when the user clicks add, as you populate the form, instead of afterwards, just before you post the form. It will be easier to deal with the input errors when the user clicks add than it will be to deal with them when they click save.
How can you use this method if you want to modify existing records in the database?
There's a few different ways you can handle this. The easiest way is to pre-populate your form with table rows for each existing row in your database, and add an id (assuming you have an auto-increment primary key for each row) input value for that record on the table row. This way when you're processing the form, you'll be able to see if it's an existing record by checking for the existence of the id in the posted data and verifying that it exists in your database. If it doesn't have an id key you know that it is a new record and you need to do an INSERT, and if it does, you can do an UPDATE or leave the record be. For DELETED rows, you'll want to loop through your POSTed data before doing any INSERTs and gather the id values that have been posted and run a query something like DELETE FROM table WHERE ID IN (<list of posted ids>). This will delete any rows that the user removed, then you can loop through the POSTed data again and insert the new rows.
An example of pre-populating this table would look something like this:
<?php
$query = "SELECT * FROM bill_items WHERE bill_id = 123";
$result = mysql_query($query);
$materials = array();
while ($row = mysql_fetch_assoc($query))
{
$materials []= $row;
}
?>
<? foreach ($materials as $material): ?>
<tr>
<td>
<?= $material['code']; ?>
<input type="hidden" name="record[<?= $material['id']; ?>][code]"
value="<?= $material['uom']; ?>">
</td>
<td>
<?= $material['component']; ?>
<input type="hidden" name="record[<?= $material['id']; ?>][component]"
value="<?= $material['uom']; ?>">
</td>
<td>
<?= $material['category'];
<input type="hidden" name="record[<?= $material['id']; ?>][category]"
value="<?= $material['uom']; ?>">
</td>
<td>
<?= $material['quantity']; ?>
<input type="hidden" name="record[<?= $material['id']; ?>][quantity]"
value="<?= $material['uom']; ?>">
</td>
<td>
<?= $material['uom']; ?>
<input type="hidden" name="record[<?= $material['id']; ?>][uom]"
value="<?= $material['uom']; ?>">
<input type="hidden" name="record[<?= material['id']; ?>][id]"
value="<?= $material['id']; ?>">
</td>
</tr>
<? endforeach; ?>
Also, a note. I changed the javascript example code above. I changed count++ to count-- because when you pre-populate the form with data that is currently in the database you are going to use the id of the material in the input key. When a user adds new data, there is a possibility that the key generated with javascript (with count++) will collide with the existing table data. To rectify this, we change it to count--. This key (in javascript) really isn't important, it's just keeping our data grouped together, so a negative value here does not affect anything.

implement a button to send information to another php file?

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.

Categories