I have an PHP page, which contains a form with some different input fields, e. g. day, month, year etc.. The form method is POST, only one non-editable field (The user ID) is sent via GET.
Of course, there is a "Submit"-Button, which triggers the form Action (PHP Script on Server).
The form tags contain a table with empty cells too. Now comes my question:
If the user clicks into one of the table cells, the form should be submitted, but additional to the regular form data the ID of the table cell should be transmitted too (If via POST or GET doesn't matter to me). How can I do that?
//Edit 2:
...
<form method="post" action="<?= DOMAIN?>/.../addUserTimetable.php?uid=<?= $user->getUserID() ?>">
<select id="day" name="day">
...
</select>
...
<input name="yearend" id="yearend" ...>
<button type="submit">...</button>
<table class="bordered">
<tr>
<th>Std.</th>
<th>Montag</th>
<th>Dienstag</th>
<th>Mittwoch</th>
<th>Donnerstag</th>
<th>Freitag</th>
</tr>
<?php
for($i=1; $i<13;$i++) {
echo "<tr>";
echo "<th>".$i. "</th>";
for($j=1;$j<6;$j++) {
echo "<td id='h".$i. "d".$j. "' onclick='???'></td>";
}
echo "</tr>";
}
?>
</table>
</form>
...
The server sided procession is fine, but I haven't got any ideas - even after two hours google - how I could transmit the cell id additionally.
That shouldn't be to hard. Have a look at the following example:
<form>
<input type="text" name="something">
<table>
<tr>
<td><input type="submit" name="cel1">
</tr>
<tr>
<td><input type="submit" name="cel2">
</tr>
<tr>
<td><input type="submit" name="cel13">
</tr>
</table>
<input type="submit" value="save">
</form>
By giving the submit buttons in the table cells a name attribute, that name will also be present as a key on the $_REQUEST. Go ahead and var_dump the $_REQUEST and you'll see you can find out in the backend which button got pushed by checking which key exists.
Note that POST / GET is completely irrelevant here, both will work just the same. And obviously you could apply some css to those buttons to make them transparent and lay them on top of the table cells, so they don't look like buttons, but just "capture" the user's click.
One last side note, are you sure you want to send the userID as a GET parameter? That would be very easy for someone with bad intentions to manipulate. Consider not sending the ID at all, but keeping it in the session on the server.
I have 2 tables, one with data and other is blank in same database.
a)- Tables "cusrec" is main and contains data in it.
b)- Tables "order" is empty and I want to insert the data in it.
I tried to fetch data from table "cusrec" and insert it into "order", when I echo, it shows the data of table "cusrec" but it is not inserting into table "order". Both tables are in same database.
Code is:
<?php
mysql_connect("localhost","root","");
mysql_select_db('dobhighat');
if(isset($_GET['search'])){
$srch = $_GET['srch'];
$que=mysql_query("select * from cusrec where custid='$srch' OR mobile='$srch'");
$ftch=mysql_fetch_array($que);
$scustid=$ftch['custid'];
$sname=$ftch['name'];
$smobile=$ftch['mobile'];
$totcloth=$ftch['clothpackage'];
if(isset($_POST['confirm']))
{
$ordernum=$_REQUEST['ordernum'];
$orderdate=date('d/m/y');
$ordercloth=$_REQUEST['ordercloth'];
$clothrem=$totcloth-$ordercloth;
$abc=mysql_query("insert into order(custid,name,mobile,totcloth,orderno,orderdate,ordercloth,clothrem)values('$scustid','$sname','$smobile','$totcloth','$ordernum','$orderdate','$ordercloth','$clothrem')");
}
}
?>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>
<body>
<?php $orddate=date('d/m/y'); ?>
<form name="form 1" action="" method="get">
<div align="right"><input type="text" name="srch" placeholder="Search by Id or Mobile" size="25">
<input type="submit" name="search" value="Search"></div>
</form>
<form name="form2" action="" method="post">
<table>
<tr>
<td width="103">Order Date</td>
<td width="94">Customer Id</td>
<td width="53">Name</td>
<td width="71">Mobile</td>
<td width="144">Order No.</td>
<td width="144">No.of Clothes</td>
</tr>
<tr>
<td><?php echo $orddate; ?></td>
<td><?php echo #$ftch['custid']; ?></td>
<td><?php echo #$ftch['name']; ?></td>
<td><?php echo #$ftch['lname']; ?></td>
<td><?php echo #$ftch['mobile']; ?></td>
<td><input type="text" name="ordernum" required></td>
<td><input type="text" name="ordercloth" required></td>
</tr>
<tr><td colspan="8"><center><input type="submit" name="confirm" value="Confirm"></center></td></tr>
</table>
</form>
</body>
</html>
Help is needed
Forget all I said about the forms and the structure of the document (although it could be useful eventually as I don't know how reliable is that GET and POST).
I am a dodo and just realized that your table name is order. ORDER is a reserved keyword in SQL and it's the reason why your SQL statement is incorrect! Just put the name between ` and it will work:
$abc=mysql_query("insert into `order`(custid,name,mobile,totcloth,orderno,orderdate,ordercloth,clothrem)values('$scustid','$sname','$smobile','$totcloth','$ordernum','$orderdate','$ordercloth','$clothrem')");
Or you may want (if possible) to rename the table in order to avoid confusion and future problems.
This doesn't mean that the code is perfect. You should still look at the recommendations by the other users and the list below, and improve it. Specially the security concerns.
Once you fix that, the insert will work; but there are still many things that you need to fix:
You should not use mysql functions and move to mysqli or PDO.
You should sanitize all the values that go to the database or to the page:
Your code is subject to SQL injection (see comments in question).
Your code is subject to XSS (see comments in question).
The doctype in your page is incorrect (if you want html5, it should be <!doctype html> and not <!doctype>.
Names and IDs must not have white spaces in them (read this for name and id notation: http://www.w3.org/TR/html401/types.html#type-name)
As stated in other answers, the SQL statement could be improved. The solution that you have may work fine (I don't find any apparent error) but it is not ideal and may present performance problems and other type of problems (e.g.: if two different users have the same phone number).
And there are probably more things that I didn't notice as I just looked quickly through the code.
Have you thought of doing an insert from select? Something like...
INSERT INTO ORDER
( custid, name, mobile, otherFields, etc... )
SELECT same, ordered, fields, as, theInsert
from custrec where custid='$srch' OR mobile='$srch'
The only issue I see with your select where clause is that you could be getting multiple customers via the OR mobile='%srch' and cause duplicate orders.
You can do it directly from SQL
INSERT INTO order
( custid,
name,
mobile,
totcloth,
orderno,
orderdate,
ordercloth,
clothrem)
SELECT
custid,
name,
mobile,
totcloth,
orderno,
orderdate,
ordercloth,
clothrem
FROM cusrec
WHERE custid='$srch' OR mobile='$srch'"
;
You just need to match the column with from table to to table
I think to insert and fetch the same data we can do it by using the below-mentioned code for PHP MYSQLI.
$id=trim($_POST['id']);
$email=trim($_POST['email'];
$env_name=trim($_POST['env_name'];
$sql="INSERT INTO integration (id,email,env_name) VALUES ($id,$email'$envName')";
if(mysqli_query($conn,$sql)){
$selectSql="SELECT id,email,env_name from integration WHERE id=$id and email='$email'";
$result=mysqli_query($conn,$selectSql){
if($result){
$row=mysqli_fetch_array($result, MYSQLI_ASSOC);
return $row;
}
}
}
I have all data from a database table display as an HTML table on a page.
When an admin selects a row, it adds a class of 'remove' to the parent row in the HTML table.
I then want to delete it from the mysqli database. Would I set the row fields as variables and delete the row that matches it in the database? I'm not sure how to do it, could someone please point me in the right direction?
HTML
<table>
<tr class="remove">
<td>field1</td>
<td>field2</td>
<td>field3</td>
</tr>
<tr>
<td>another field</td>
<td>another field</td>
<td>another field</td>
</tr>
</table>
Mysqli (relevant sample)
$mysqli->query("DELETE FROM $table WHERE Date="??" LIMIT 1");
If you're using JavaScript, which I assume you are as you're adding a class to a row when it's selected, you could do the following:
1. Include a hidden disabled input in each row:
<tr class="remove">
<td>field1</td>
<td>field2</td>
<td>
field3
<input type="hidden" disabled name="delete[]" value="<?php echo $id; ?>">
</td>
</tr>
2. Enable the hidden input when it is selected to be removed:
I'm not going to guess how you're adding the remove class to the row, but when doing so have it remove the disabled attribute of the hidden delete[] input contained within it.
3. Post the form:
Once posted you can run the query to delete all the rows with an ID in the $_POST['delete'] array.
If you're using prepared statements you can use the following query where ? is implode(',', $_POST['delete']):
"DELETE FROM " . $table . " WHERE FIND_IN_SET(id, ?) != 0"
I'm assuming here for the sake of simplicity that you have a unique id field (that doesn't contain ,s as part of its value). Obviously change to whatever is relevant for your database.
I am trying to get those data in the JSON format. Would you please help me to generate that? Please help I am not very familiar with jquery and php.
html code looks like this
<form>
<table>
<tr>
<td>//some code for other table elements</td><tr>
<tr>
<td>Critical Times (Coverage): </td>
<td rowspan="3" colspan="2">
<div>
<table width="100%" bgcolor="white" border="0px" name="criticalTime" id="criticalTime">
</table>
</div>
</td>
<td>
<input type="button" id="timeAdd" value="+"/><br>
<input type="button" id="timeRemove" value="-"/>
</td>
</tr>
<tr>
<td>//some code for other table elements</td>
</tr>
</table>
</form>
my .js file has the following to handle the addition of rows to the table when click the + button.
var rowCountr = 0;
var timeTable="<thead><tr bgcolor='E1ECFF'><th></th><th align='left'>Start Time</th><th align='left'>End Time</th></tr></thead>";
/* insert the html string*/
$("#criticalTime").html( timeTable );
/*Event for the +(Add) button in critical time table*/
var timeRwcntr=0;
timeRow="<tr><td><input type='checkbox' id='row'</td><td><input type='text' id='start' /></td><td><input type='text' id='end'/></td></tr>";
$('#timeAdd').click(function(){
$('#criticalTime').append(timeRow);
rowCountr++;
timeRwcntr++;
});
$('#timeRemove').click(function(){
$('#criticalTime tr:last-child').remove();
});
When I click the submit button from main form I need to get those values entered in the input fields and generate an JSON array to store those in database. Please help.
For this problem, I think that JSON may be overkill. You're just trying to send your table's input fields back to the server on a postback right? When you create your table rows, add a uniquely identifying number in the "name" attribute of your form controls <input name="'.$id.'_field" /> for example if you are rendering the table server-side; the code would be similar for javascript. Then, when you post back, you can loop through each row by counting and then access the POST variable you got back by referencing the name. Example:
for ($i = 0; $i < $rowcount; $i++) {
$field_value = $_POST[$i.'_field'];
...
}
JSON is great for more complex data sets, but in your case it sounds like you have structured two-dimensional rows and columns.
dear all..i have a textfield
<tr>
<td>
<td><input type="text" id="model_name"></td>
</td>
</tr>
and a cell
<tr>
<td><div id="value">//i want data show here after fill textfield</div>
</td>
</tr>
beside that, i've a table "settingdata" in database it consist of 2 field:itemdata and remark..
itemdata's value are "UD" and remark's value are "FM=87.5-108.0MHZ"...
what must i do if i want after type model name "car01UD" at textfield inside
<div id="value"></div>
can show "FM=87.5-108.0mhz"...
if you want "0103" + value of itemdata then do following
<input type="text" id="mod" value="0103<?PHP echo $itemdata ?>">
Looks like you are trying to do basic Ajax functionality. Consider reading some Ajax tutorials to get an idea how to do this.