form submit is not working when i click button - php

Why isn't my form working and why are there no errors showing? When I click the submit button nothing happens, here is my code:
<?php
$list_of_product=$p->loadSearchProduct($searchkey);
foreach ($list_of_product as $product) {
echo "<tr>"
.'<form id ="myForm" action = "InsertOrders.php" method = "POST">'
."<td>".$product->prodname."</td>"
."<td>".$product->price."</td>"
."<td>".$product->total." "."Piece(s)"."</td>"
."<td>".$product->remaining_stock." "."Piece(s)"."</td>"
."<td>".$product->sold_stock."</td>"
."<td>"."<input class='form-control' name='qty' type='text' required/>"."</td>"
."<td>"."<button type='submit' id='sub' class='btn btn-primary'>"."<b>Add to List</b>"."</button>"."</td>"
."</form>"
."</tr>";
}
?>

In this case you have several forms with the same id.
When you click submit, it es not specific witch one is clicked.
EDIT
$count = 0;
foreach ($list_of_product as $product) {
echo "<tr>"
.'<form id ="myForm" action = "InsertOrders.php" method = "POST">'
."<td>".$product->prodname."</td>"
."<td>".$product->price."</td>"
."<td>".$product->total." "."Piece(s)"."</td>"
."<td>".$product->remaining_stock." "."Piece(s)"."</td>"
."<td>".$product->sold_stock."</td>"
."<td>"."<input class='form-control' name='qty' type='text' required/>"."</td>"
."<td>"."<button type='submit' id='sub".$count."' class='btn btn-primary'>"."<b>Add to List</b>"."</button>"."</td>"
."</form>"
."</tr>";
$count++;
}
When you use an counter, you can check in you InsertOrders.php which one is clicked. Its not the best way, but it will be work.

i think if you can use form tags outside of the foreach, it will submit. Or else try remove 'id' attribute in your code.

Related

Differentiate between multiple buttons on a table. html php

I am creating an edit edit/delete user table and have created an 'edit' button for each record populated in the table. I want to do several things.
1. when an edit button is pressed for a specific user, open a new page called "EDIT."
2. populate form controls in the "EDIT" page with the corresponding user information for the specific 'edit' button that was pressed.
My question is, how do I differentiate between which button is pressed on the users table?
this is what my table looks like:
And this is the code for generating the table and buttons.
if (!$_REQUEST['search']) {
$sql = "SELECT * FROM users_table ORDER BY lname ASC";
$retvalues = mysqli_query($conn, $sql);
$counter = 1;
while ($row = mysqli_fetch_array($retvalues, MYSQL_ASSOC)) {
$lname = capword($row['lname']);
$fname = capword($row['fname']);
if ($row['admin'] != 1) {
$stringAdmin = "No";
$admincolor = "<td>";
} else {
$stringAdmin = "Yes";
$admincolor = "<td style='color:red;'>";
}
echo "<tr>";
echo "<td>".$counter.".</td>";
echo "<td>".$lname." , ".$fname."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['password']."</td>";
echo $admincolor.$stringAdmin."</td>";
echo "<td><input type='checkbox' name='user[]' value='{$row['id']}'></td>";
echo "<td><input type='submit' name='edit' value='edit'></td>";
echo "</tr>";
$counter++;
}
}
You can differentiate the each edit button by using unique id
<button class='edit' data-id="<?php echo $_GET["id"];?>"> EDIT</button>.
While click on the edit button, read the data-id by using the following code
$(document).on('click', '.edit', function(){
var id = $(this).attr('data-id');
//code - you need to do
})
Better to show the data on next page, use bootstrap model box and AJAX request which will interact lot of the users.
$(document).on('click', '.edit', function(){
var id = $(this).attr('data-id');
if(id) {
$.ajax({
url : your url("profile/edit/"+id)
type : 'post',
success: function(response){
if (response.success) {
$('#modal').modal('show');
}
}
}):
}
});
If you change the value attribute of "Edit" submit button to the row id you can use this value to know what record id will be edited and populate it.
"<input type='submit' name='edit' value='edit'>"
change to
"<button name='edit' value='{$row['id']}'>Edit</button>"
Note that if the whole table is the form container, all inputs in table will be posted, not only the current row.
I would make the edit button into a hyperlink instead. Then there's no need to have form code. I'm assuming you don't need to pass the "delete" parameter to your edit page (as that's a different operation).
Instead of
echo "<td><input type='submit' name='edit' value='edit'></td>";
write
echo "<td><a href='edit.php?id=".$row['id']."'>Edit</a></td>";
Then in edit.php look for the variable
$_GET["id"]
and use that to search the database and display the appropriate record for editing.
P.S. If you still want your "Edit" hyperlink to look like a button it's quite easy to do that with CSS.
I did this by creating and calling a javaScript function that passes the id of the row as an argument.
echo"<tr>";
echo"<td>".$counter. ".</td>";
echo"<td>".$lname. " , " .$fname."</td>";
echo"<td>".$row['email']."</td>";
echo"<td>".$row['password']."</td>";
echo $admincolor . $stringAdmin ."</td>";
echo"<td><input type='checkbox' name='user[]' value='{$row['id']}'></td>";
echo"<td><input type='button' name='edit' value='edit' onclick='javascript:editUser(". $row['id'].");'></td>";
echo"</tr>";
the function redirects the user to an 'edit' page while passing the value of the id in the url.
function editUser(id){
window.location = "edituser.php?id="+id;
}
once on the edit page, i used $_GET to retrieve the id value and edit my entry.
You can change and add a form to your table like this:
echo "<tr>";
echo "<td>".$counter.".</td>";
echo "<td>".$lname." , ".$fname."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['password']."</td>";
echo $admincolor.$stringAdmin."</td>";
<form action="edit.php" method="post">
echo "<input type="text" name="id" value="$row['id']" style="visibilty: hidden;">";
echo "<td><input type="checkbox" name="delete"></td>";
echo "<td><input type="submit" name="edit" value="edit"></td>";
</form>
echo "</tr>"
First we have defined a form with POST method: <form action="edit.php" method="post"> and let's say edit.php will handle the request.
Then we have added a hidden form element to store and pass the user's ID: <input type="text" name="id" value="$row['id']" style="visibilty: hidden;"> It was possible to use value="..." of "edit" button but if you are dealing with passing more than one variables with POST or GET method, the trick is using an extra form element with visibility: hidden; CSS property.
Eventually, it will pass the value of $row['id'] to edit.php when the form has been submitted.
and we can handle the request in edit.php like this:
<html>
<body>
User with this ID number: <?php echo $_POST["id"]; ?> will be edited.
</body>
</html>
Official guide: http://php.net/manual/en/tutorial.forms.php
You can either use POST or GET method to pass the variables. For a detailed comparison: https://stackoverflow.com/a/504993/2104879

array of submit buttons in different forms generated with php

I dynamically create html forms with a loop in php and each submit button is assigned a name as part of an array. How can I check which submit button is set and get its value? I tried this code but it doesn't work.
<?php
if($count_eksp){for($i=0; $i<$count_eksp; $i++){
$fusha_eksp = mysql_fetch_row($query1);
echo "<br>$fusha_eksp[2] $fusha_eksp[3]<form method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'><input name='eksp_edit[]' type='submit' value='$fusha_eksp[0]' height='20' width='20' ><input id='eksp_fshi[]' type='image' src='fshi.png' height='20' width='20'></form>";
}}
?>
<?php
if(isset($_POST['eksp_edit[]'])){
foreach($_POST['eksp_edit'] as $id){
$query = mysql_query("DELETE FROM `fusha_ekspertizes` WHERE `id`='$id'", $db_server);
}
}
?>
You have error in if condition, in the key. Remove [] for that.
if (isset($_POST['eksp_edit'])) {
...
}
The second error is in action attribute, you can have another <? inside echo. When the form is processed on the same page, you can leave this attribute.
echo "[...]<form method='post'><input [...]";

Send column name from button

I have made a php page. On which I am displaying table 'prod' from my data base.
each row is displayed nicely. Today i tried to add a button named 'rate' at the end of each row of my table. which I did successfully. Now I want to send the the value of the first column of that row to another php page when that button is clicked. I am stuck that how to do so? can you help please ??
I know i have to use the method post in my form and i have to use $_post[that value] on the other php page to inculcate the value for further function.
I just need to ask that where to add the value of my first column in the button line. so that onclick it can send that value. I hope I am clear over this. Thank You very much for help :)
<?php
include("connection.php");
$query = "select * from prod";
$res = oci_parse($conn,$query);
usleep(100);
if (oci_execute($res)){
usleep(100);
print "<TABLE border \"1\">";
$first = 0;
while ($row = #oci_fetch_assoc($res)){
if (!$first){
$first = 1;
print "<TR><TH>";
print implode("</TH><TH>",array_keys($row));
print "</TH></TR>\n";
}
print "<TR><TD>";
print #implode("</TD><TD>",array_values($row));
print "</TD></TR>\n";
echo "<td><form action='detailform.php' method='POST'><input type='submit' name='submit-btn' value='Rate'/></form></td></tr>";
}
print "</TABLE>";
}
?>
you have to add inputs in your form whatever kind u prefer
echo "<td>
<form action='detailform.php' method='POST'>
<input type='hidden' name='your_val_key' value='".$row[your_val_key_in_query]."'> <!-- input hidden, change to text 4 debug -->
<input type='submit' name='submit-btn' value='Rate'/>
</form>
</td></tr>";
and than, in your detailform.php u can get the val with
echo $_POST["your_val_key"];
if u are not sure how much data u send or somthing, try this and u get the full data:
echo "<pre>".print_r($_POST,true)."</pre>";
BTW: why are u mixing print and echo?
Use hidden input
echo "<td><form action='detailform.php' method='POST'><input type='hidden' name='col-name' value='you-col-value'><input type='submit' name='submit-btn' value='Rate'/></form></td></tr>";

Inserting Image to MySQL Database

I am working on a solution that will help me submit specific images from a list of images to a MySQL Database.
My database consists of the following:
Database
id(INT)
photo(BLOB)
caption(VAR)
Code
I am first retreiving images from a list and giving them each a submit button.
foreach ($media->data as $data) {
echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
echo "<form action='tag.php' method='post'>";
echo "<input type='submit' name='submit' value='Click Me'>";
echo "</form>";
}
$pictureImage parses the data URL and then puts it into an actual image.
The submit button is below each of those images.
I am then making it so that when the submit button is pressed, it is added to the database.
if(isset($_POST['submit'])) {
//Database code would be above the following
$sql="INSERT INTO $usertable (image) VALUES ('$pictureImage')";
}
Problem
I am running into an issue where the last image in my list is the one being submitted to the database, rather than the image with the corresponding submit button. How do I make it so that it is grabbing the photo with the corresponding submit button?
Any help would be appreciated greatly.
You need to include any identifier to the image inside the form in order to store it.
For example you can try building the forms like this:
foreach ($media->data as $data) {
echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
echo "<form action='tag.php' method='post'>";
echo "<input type='hidden' name='imageid' value='{$data->images->thumbnail->url}'>";
echo "<input type='submit' name='submit' value='Click Me'>";
echo "</form>";
}
And when you want to store the image on the form submit you can actually pick up the identifier of the image (in this case I used the URL you posted):
if(isset($_POST['submit'])) {
$sql="INSERT INTO $usertable (image) VALUES ('$_POST[imageid]')";
}
You are not posting any data when you click on your submit buttons.. i would suggest that for each form you would have a hidden field with the url of the image.
something like this:
<form action='tag.php' method='post'>
<input type="hidden" value="{$data->images->thumbnail->url}" name="pic"/>
<input type='submit' name='submit' value='Click Me'>
</form>
Problem
foreach ($media->data as $data) {
echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
echo "<form action='tag.php' method='post'>";
echo "<input type='submit' name='submit' value='Click Me'>";
echo "</form>";
}
By the time this loop is done, you will have the last image in the loop as the value for $pictureImage.
So when it gets to this point, you're $pictureImage is still... the last image.
if(isset($_POST['submit'])) {
//Database code would be above the following
$sql="INSERT INTO $usertable (image) VALUES ('$pictureImage')";
}
Solution
I'm not exactly sure what data value you want to save because right now it looks like the whole tag. But whatever it is, you need to put it into a form field first ...
foreach ($media->data as $data) {
..
echo "<form action='tag.php' method='post'>";
echo "<input type='hidden' name='imageurl' value='<img src=\"{$data->images->thumbnail->url}\">' />";
..
}
and then retrieve it in the POST part of your script:
if(isset($_POST['submit'])) {
$imageurl = $_POST['imageurl'];
//Database code would be above the following
$sql="INSERT INTO $usertable (image) VALUES ('$imageUrl')";
}
This way, it won't always be the last value in your list, rather, it will be the one you selected.

Need javascript confirm box dialogue & delete images when press ok

<?php
require "../db/dbconfig.php";
$gal=mysql_query("select * from gallery");
$numrows=mysql_num_rows($gal);
if($numrows>=1)
{
echo "<form action='delete.php' method='post' name='f2' id='f2'>";
echo '<table id="rqst" style="display:block;" border="0" cellpadding="12" cellspacing="3" width="500px">';
echo "<tr><th>Tick to select</th><th>Images in Gallery</th></tr>";
while($row=mysql_fetch_array($gal)
{
$imgfile=$row['ImgName'];
$Image="<img src=gallery/".$imgfile." width='230px' height='150px'/>";
$img_name=$imgfile;
echo "<tr>";
echo "<td><input type='checkbox' name='imgs[]' value='$img_name'></td><td>".$Image."</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan='3' align='right'>";
echo "<input type='submit' value='Delete' name='del'></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
?>
This is my code....This will display images from gallery and checkboxes associated with them. When I click delete button with unchecked checkboxes an alert should come like this "Please check at least one checkbox"..How to do that??
My next problem is,,when I click delete button after checked checkbox, alert should come like this=" Do you want to delete?? "...If clicked Ok,the image must be deleted else do nothing...Please help ...Thanks in advance....
check this below link for validation using jquery:
http://jsfiddle.net/susheel61/U3Unk/2/
<form id="form">
First name: <input type="checkbox" name="firstname" class="check"><br>
<button>test</button>
</form>
$("button").on("click",function(e){
var status = $(".check").is(":checked");
e.preventDefault();
if(!status){
alert("this is not checked");
}
});
Yes, you can do it by javascript or jquery to validate whether your atleast one checkbox is select or not. So, for that you need to give a common class for all checkbox as example
<input type="checkbox" name="firstname" class="addchk">
Now in your submit button call a javascript function which validate the matter.
<input type='button' value='Delete' name='del' onclick='delete_checked()' />
Now write a function to validate whether any checkbox is selected.
function delete_checked()
{
var status = $(".addchk").is(":checked");
e.preventDefault();
if(!status){
alert("this is not checked");
}
else
{
// SUbmit yout form
}
}

Categories