I have a form with PHP language and I want to use it for inserting data, updating data, and deleting data at once. First row of table is for insert data, second table and other row is for edit or delete data.
So, I have create form like this:
FYI : UBAH is Update/Edit, HAPUS is Delete, TAMBAH is Add/Insert
I've already done with inserting data, but I'm stuck while Updating or deleting data because the ID that the button I choose is always at last ID (in picture is the ID always refer to "Sarjana") so it doesn't works because I can only update and delete the last data that called at table.
So, what I want to ask is, how to make it works without AJAX? Because what I search in Google, they said it can be done with AJAX, but I don't understand AJAX (except it will stuck forever without ajax). And I don't want to create an href link just like "beginner example" that throw it to another pages then edit/delete it.
Note: It's just like a listview at ASP but I want to do it at PHP code. Final Words, I want to create Inline editing at my PHP code.
I hope You understand and sorry for my bad English.
Add a unique id to each submit button:
<input type="submit" name="submit[1]" value="Submit">
<input type="submit" name="submit[2]" value="Submit">
<input type="submit" name="submit[3]" value="Submit">
Then you grab the id from $_POST or $_GET. For example:
Array
(
[submit] => Array
(
[3] => Submit
)
)
Edit:
Take this concept - HTML form elements to PHP arrays - and apply it to the other elements.
<table>
<tr>
<th>Program</th>
<th>Department</th>
<th></th>
</tr>
<tr>
<td><input type="text" name="program[1]" value=""></td>
<td><input type="text" name="department[1]" value=""></td>
<td>
<input type="submit" name="edit[1]" value="Edit">
<input type="submit" name="delete[1]" value="Delete">
</td>
</tr>
<tr>
<td><input type="text" name="program[2]" value=""></td>
<td><input type="text" name="department[2]" value=""></td>
<td>
<input type="submit" name="edit[2]" value="Edit">
<input type="submit" name="delete[2]" value="Delete">
</td>
<tr>
<td><input type="text" name="program[3]" value=""></td>
<td><input type="text" name="department[3]" value=""></td>
<td>
<input type="submit" name="edit[3]" value="Edit">
<input type="submit" name="delete[3]" value="Delete">
</td>
</tr>
</table>
Again: use the unique IDs to set each row's form elements apart.
Here's one method for grabbing the data in PHP. Obviously you need to get the ID from the submit button and then use that ID to reference elements in the $_POST or $_GET array.
if ( isset($_POST['edit']) && is_array($_POST['edit']) ) {
$key = key($_POST['edit']);
$program = $_POST['program'][$key];
$department = $_POST['department'][$key];
}
Related
i want to get the text box value which i selected from array of text boxes..
My code is like this:
Here am displaying the data on the browser from DB..
<?php
foreach($dataDecoded['categories'] as $key => $value)
{
?>
<tr>
<td><?php echo $value['CategoryName'];?></td>
<td>
<input type="text" name="categoryId[]" id="categoryId" value="<?php echo $value['CategoryId']?>">
<input type="text" name="categoryName[]" id="categoryName" value="<?php echo $value['CategoryName']?>">
<input type="submit" name="create" id="create" value="create">
</td>
</tr>
<?php
}
?>
The data will be displayed like:
categoryId1 categoryName1 <Create Button>
categoryId2 categoryName2 <Create Button>
categoryId3 categoryName3 <Create Button>
and so on like this..
Now, suppose When i click on Create Button of CategoryName2, i want to get the categoryId and categoryName of only CategoryName2..
Am using this code:
if(isset($_POST['create']) && $_POST['create']=="create")
{
$cat_id[]=$_POST['categoryId'];
$cat_name[]=$_POST['categoryName'];
}
But with this, am getting all the categoryId and categoryName into the array.. how to get only the selected textbox value..
I want to do it using only PHP and not with JavaScript / JQuery... Am a bit new to PHP... Can someone help me / give me a suggestion about how to do it...
Thanks in advance.
Wrap each one in its own form:
<td>
<form method="POST" action="somePage.php">
<input type="text" name="categoryId" value="<?php echo $value['CategoryId']?>">
<input type="text" name="categoryName" value="<?php echo $value['CategoryName']?>">
<input type="submit" name="create" value="create">
</form>
</td>
Since the content being posted in this case is just these two values, then that's the entire scope of a single form post. So each one is logically/semantically its own form. (There's no rule that says a page can have only one form. Unless you're ever using ASP.NET WebForms, which is a lie.)
Note also that I removed your id attributes. Multiple elements can't have the same id, that's invalid HTML and any behavior as a result becomes undefined.
Please help me out of this.....
I am designing a table which is inside a form.
the table is generated based on while loop.
In each row there is a download button.
when i click download the POST value should get the same row information.
But my POST variable is giving me the last row information only.
I tried using input-type as hidden... But it did not work
Here is the code for your reference
enter code here
<form name="simpleform" method="post" action="insert.php">
<?php
$data = "environment";
$user_name = $_SESSION['username'];
$serch = mysql_query("SELECT * FROM data WHERE (data_category = '" . $data . "') ");
while ($record=mysql_fetch_assoc($serch))
{?>
<tr class="warning">
<td >
<input type="text" value=<?php echo $record['data_ID'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_ID'];?> name="dataid" />
</td>
<td >
<input type="text" value=<?php echo $record['data_name'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_name'];?> name="dataname" />
</td>
<td >
<input type="text" value=<?php echo $record['data_downloads'];?> readonly="readonly">
<input type="hidden" value=<?php echo $record['data_downloads'];?> name="datadown" />
</td>
<td >
<input type="text" value="" >
<input type="hidden" value="" name="datause" />
</td>
<td>
<input type="submit" name="simplesubmit" value="Go to download" />
</td>
</tr>
<?php }
exit;
?>
</tbody>
</form>
The problem is that you are using the same name attribute for all your controls. Thus, when PHP receives the form, they get overwritten, and you only see the last value of the form.
The simplest way to avoid that is just appending [] to the end of your names -- eg name=dataid[]. This will make PHP take all arguments as an array, so you don't lose data.
The second problem, is that your submit button also has the same name - you should diversify it by using some row-specific data in its name, such as 'name="submit-'.$record['data_name'].'"'
For more info, more code from you is needed, such as what are the data you are printing like.
Every post button can have its name and value, so if you change your code to produce a traceable post button name you can just do whatever you want with that.
<table>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[1]" value="OK" />
</tr>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[2]" value="OK" />
</tr>
</table>
When the form is posted its very easy to capture which button is clicked;
if ($_POST["submit"]) {
$id = key($_POST["submit"]);
}
Thanks for info... and good response. As you said , i replaced the same and saw the post value is giving me all arguments as array. My purpose is to let the client download file that he clicks. so if the client click the first row button in the table, the post value should get only that Data_name. So that i can run a query to get the URL of that data_name and download
I've made the form below. Is it possible to make it that when user enters the number of fields, for example 6, that the table below has 6 rows. It would be great if it would be possible to make it without any submit button (so that the trigger for this action is exiting from the text input box).
Here is the html code of this form:
<fieldset>
<legend>Student Information</legend>
Number of fields: <input type="text"><br />
Total number of characters: <input type="text">
<br>
<br>
<table border="1">
<th></th>
<th>field</th>
<th>number of characters</th>
<tr>
<td>1</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>2</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
</table>
</fieldset>
If this is not possible (without submit button), than in which way would you accomplish the same result? Thank you for any help and/or suggestions.
PHP is server side, it runs only once, when the page is loading. HTML is not a programming language. You could generate the table with PHP, but only if you had a submit button that reloaded the page. If it has to happen because of a user event, it always needs to be done with Javascript.
That means, you will need Javascript to make this work without reloading the page. Ideally, you would use Jquery (Javascript's most popular plugin) to manipulate the DOM.
If you had this input :
<input id="field" type="text">
You could call the on-leave event like this :
$("p").focusout(function()
{
// Delete the previous table, and create a new one, here
});
As for creating the actual table, it isn't complicated, but it is a bit of work. You should read the following reference to start you up :
http://www.tutorialspoint.com/jquery/jquery-dom.htm
You will need to "install" JQuery before-hand, you can simple insert this at the top of your code :
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
Okay here is the post only script you require
<?php
$rows=2;
if(isset($_POST['submit']))
{
if($_POST['submit']=='Update')
{
if(isset($_POST['rows'])) $rows=max($rows, intval($_POST['rows'])); // minimum 2 rows
}
else
{
// process posted data here
// reset post or jump to another page
$_POST=array();
//header("Location:index.php");
//exit();
}
}
?>
<form method="post">
<fieldset>
<legend>Student Information</legend>
Number of fields: <input type="text" name="rows" value="<?php echo $rows; ?>"><br />
Total number of characters: <input type="text">
<input type="submit" name="submit" value="Update"/>
<br>
<br>
<table border="1">
<th></th>
<th>field</th>
<th>number of characters</th>
<?php
for($loop=1;$loop<=$rows;$loop++)
{
echo '<tr>';
echo '<td>'.$loop.'</td>';
echo '<td><input name="field['.$loop.']" value="'.$_POST['field'][$loop].'" /></td>';
echo '<td><input name="chars['.$loop.']" value="'.$_POST['chars'][$loop].'" /></td>';
echo '</tr>';
}
?>
</table>
<input type="submit" name="submit" value="Submit"/>
</fieldset>
</form>
It will default to 2 rows (minimum), and retain the data when you update the rows.
If the rows get reduced, then the end ones disappear
It certainly would be doable with just PHP.
So for example, if you typed in '6' rows you could catch the form post and do something like (template form for within the HTML):
<?php for($i=0; $<=$_POST['rows'];$i++): ?>
<!-- This being your whatever html for the table -->
<tr><td></td></tr>
<?php endfor; ?>
I am new to web development
I have PHP delete function that I want to use to delete rows from a database table.
if($form_no==="delete" && isset($_POST['id']) && isset($_POST['form_no']))
{
switch ($_POST['form_no']) {
case 1:$table="employees"; break;
default:break;
}
$query="DELETE from ".$table." where id=".$_POST['id'];
mysql_query($query)or die( ' '.mysql_error().' '.$query);
$message='deleted';
switch ($_POST['form_no']) {
case 1:manageEmployees($message);break;
default:break;
}
}
here is one of the forms that I am displaying on screen
<tr><th>First Name</th><th>Last Name</th><th>Email</th><th>Password</th><th>Phone number</th></tr>
<tr><td><input type="hidden" id="form_no" name="form_no" value="1"></td></tr>
<tr><td><input type="text" name="firstname[]" id="firstname"></td>
<td><input type="text" name="lastname[]" id="lastname"></td>
<td><input type="text" name="email[]" id="email"></td>
<td><input type="text" name="password[]" id="password"></td>
<td><input type="text" name="phone_no[]" id="phone_no"></td>
<td><button type="submit" name="submit" id="submit">Add Employee</button></td>
<td><button type="button" name="delete" class="delete" value=21>Delete Employee</button></td></tr>
<td><p><p></td>
</tr>
I am trying to get access to other values in a form. When the button is clicked I can get the ID I am looking for as it is the value of the button. What I really want to do is get the value of the hidden field and find out in which for the delete button is pressed.
I can not use find elementById as the delete buttons are not unique nor are the forms. I am trying to use $(this).parents().parents('#form_no').val(), however this returns undefined.
I know that it must be possible to do but I am not getting any closer, many thanks in advance if any one can point me in the right direction.
So I am at my wits end. Out of the more complex issues, this is the one that has just stumped me. I'm not sure if it's a HTML issue or a PHP issue, anymore. I've tried everything I could think up, and just nothing.
Ok so I need to pull a specific name out of a dynamically created table, the Weapon’s Name to be exact.
So User clicks the button “Delete weapon” and that weapon would be deleted. The query is fine; my issue is getting this name. If there was just one weapon and this wasn’t dynamic I would just go for the specific field name. That’s PHP 101.
This is the loop it's being pulled from:
while ( $weapons = mysql_fetch_array($data)) {
echo '
<table width="780" border="1">
<tr>
<td colspan="2">weapon</td>
<td>AttackBonus</td>
<td>crit</td>
</tr>
<tr>
<td colspan="2">
<input type="text" name="weaponName'.$i.'" id="weaponName'.$i.'" size="10" value="'.$weapons["weaponsName"].'" />
<input type="hidden" name="weaponName" value="'.$weapons["weaponsName"].'"/>
</td>
<td>
<input type="text" name="attackBonus'.$i.'" id="attackBonus'.$i.'" size="10" value='.$weapons["weaponsAttackBonus"].' />
</td>
<td>
<input type="text" name="crit'.$i.'" id="crit'.$i.'" size="10" value='.$weapons["weaponsCritical"].' />
</td>
</tr>
<tr>
<td>type</td>
<td>range</td>
<td>ammunition</td>
<td>damage</td>
</tr>
<tr>
<td>
<input type="text" name="type'.$i.'" id="type'.$i.'" size="10" value='.$weapons["weaponsType"].' />
</td>
<td>
<input type="text" name="range'.$i.'" id="range'.$i.'" size="10" value='.$weapons["weaponsRange"].' />
</td>
<td>
<input type="text" name="ammunition'.$i.'" id="ammunition'.$i.'" size="10" value='.$weapons["weaponsAmmunition"].' />
</td>
<td>
<input type="text" name="damage'.$i.'" id="damage'.$i.'" size="10" value='.$weapons["weaponsDamaage"].' />
</td>
</tr>
<tr>
<td colspan="4">
<input type="submit" formaction="include/deleteweapon.php" formmethod="post" value="Delete Weapon"/>
</td>
</tr>
</table>
';
$i++;}
It’s probably something small, that’s how it’s always been my issue. I’ve tried a few different while loops. I’ve tried if statements with issets($_post[“weaponName0”], etc. I just don’t know.
Edit:
So I'm not very good with explaining what I need. The loop to display the information is fine, and yes the button needs to be there.
(I don't have enough rep to up load an image so here is the link)
http://farm9.staticflickr.com/8391/8468562067_0cd4158e35_b.jpg
This is what it looks like. I can not get the value of the weapon name. The output is fine here, it's when I need to delete that specific weapon, that I'm having issue with. I need that name. This is why I'm not sure if it's the HTML or the PHP. The button has to be iterated or whats the point? The mySQL is fine, that was the easy part.
I have an idea of what I can do, but in my opinion it's bad design and I think I would just have the same problem. I could add a check box for these fields to delete, and it'll be deleted after you try to save the character.
I'm not 100% sure I understand your problem, I believe in your HTML, you should do something like:
<input type="hidden" name="weaponName[<?php print $i; ?>]" value="The Name" />
In your PHP, you should be able to access the name given an index:
$index = 1; // Some arbitrary index
$weapon_name = $_POST['weaponName'][$index];
Alternatively, and what I would do: In your form's action, specify an id:
<form action="delete.php?id=<?php print $weaponId; ?>" action="post">
Then in your PHP, you can do:
$id = $_GET['id']; // This is the weapon ID
Then you can just pull the name directly from the database, using the ID.
It looks like you have a single delete button for each iteration of the loop. You should be able to set the name for deletion just as you set it as a label.
<input type=hidden name=weaponsName value=".$weapons['weaponsName'].">
<input type=submit ...>
$weaponsName = $_POST['weaponsName'];
You are using mysql_fetch_array instead of mysql_fetch_assoc, so you should use the field index, like this:
$weapons["weaponsName"] // This is wrong
$weapons[0] // This is the right approach
PS: You should not use the mysql extension....go for the mysqli or PDO ;)
Saludos.