PHP Pulling a specific name from a dynamically created table - php

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.

Related

Create Update Command at Same form With Many Data

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];
}

PHP POST value from a looped table

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

Is this possible to accomplish with html and php?

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; ?>

How can I populate a textarea field with various field data from the web form?

We've inherited a php/CodeIgniter app. Without going into all the reasons why, I need to feed values into a textarea field so I can group a bunch of data together and send it to a field in another app/outside vendor. This is the first time we have encountered this issue, but I don't think it is the last, so I want to prepare for it.
The specifics:
Web form with a bunch of fields on it. It's a self generating php/CodeIgniter app that the client controls, so fields are different from client to client.
Certain clients may need to send data from 3, 5, 7, etc., of the field within their form to an external vendor who receives all the data in one field on their end. So in short, using jQuery, I want to send data from certain fields to a textarea field.
For example, I want to send Center Title, Full Name, and Fruits to the textarea field with a line break at the end of each. Of course, if the user empties a field, that line item would be removed from the textarea field.
Click here to view my jsFiddle demo.
HTML Example:
<form method="post">
<br />
<br />
<fieldset name="Group1" style="border: thin; border-color: green">
<legend>General Information</legend>
<table style="width: 100%">
<tr>
<td style="width: 249px">Center Title:</td>
<td>
<select name="centers" id="centers">
<option value="Corp 1">Corp 1</option>
<option value="Shamrock Gold">Shamrock Gold</option>
<option value="Hensin Way">Hensin Way</option>
</select>
</td>
</tr>
<tr>
<td style="width: 249px">Full Name:</td>
<td>
<input name="fullname" id="fullname" type="text" size="20" />
</td>
</tr>
<tr>
<td style="width: 249px">Job Title:</td>
<td>
<input name="jobtitle" id="jobtitle" type="text" />
</td>
</tr>
<tr>
<td style="width: 249px">Known Alergies:</td>
<td>
<input name="knownAllergies" id="knownAllergies" type="checkbox" value="Yes" />Yes
<input name="knownAllergies" id="knownAllergies" type="checkbox" value="No" />No
</td>
</tr>
<tr>
<td style="width: 249px; height: 102px;">How Many?:</td>
<td style="height: 102px">
<select multiple="multiple" name="Select2">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
</select>
</td>
</tr>
<tr>
<td style="width: 249px">Fruits:</td>
<td>
<input name="Fruit[]" id="Fruit[]" type="radio" checked="checked" value="Apple" />Apple<br />
<input name="Fruit[]" id="Fruit[]" type="radio" value="Orange" />Orange<br />
<input name="Fruit[]" id="Fruit[]" type="radio" value="Fruit" />Fruit
</td>
</tr>
</table>
<label>Complete Info:</label>
<textarea name="allVendorInfo" id="allVendorInfo" cols="50" rows="7"></textarea><br />
<br />
</fieldset>
</form>
​
You can access selections/user-input using following code
$(document).ready(function(){
$("input").change(function(e){
if($(this).is(':checked')){
}else{
};
var old = $('#allVendorInfo').val();
$('#allVendorInfo').val(old+ "%" + $(this).attr('name') + '|'+$(this).val() + "%");
});
$("select").change(function(e){
var old = $('#allVendorInfo').val();
$('#allVendorInfo').val(old+ "%" + $(this).attr('name') + '|'+$(this).val() + "%");
});
});​
EDIT: Original fiddle was broken for some reason, have updated the link
I was thinking something along the lines of this: http://jsfiddle.net/JRwzz/3/
The 'trigger' for this could be something other than the Run JS button of course, that's just there for example, i'd imagine it'd be on submit or other user action.
It clears the textarea first, then loops all of the input's and selects, depending on the type of element - checkbox, radio, select etc.. it uses slightly different methods to get the values (e.g. if it's a checkbox it only wants to get the value from a checked one)
It'll need a bit of polish in order to have a checkbox group's values all on one line and things like that, but hopefully this is enough for you to get the idea.
Then on each thing it finds it appends it to the textarea and puts a linebreak on the end.
It wouldn't be too hard to add a condition to check for another atribute (e.g. data-export="yes") to check for before including it in the textarea.
Just to note, I thought of it this way because you said all of the forms are dynamic, so I tried not to need to rely on ID's or names for things, it'll just apply to any form. If you can get the code that generates your forms to output an attribute in the html on the ones you want included in your textarea (and perhaps some method of your client selecting which ones that'll apply to in their administration area) then that'd be spot on, would save having to fiddle JS for every client.
Try something like this:
$("select, input").bind("change keyup", function() {
var form = $("<form>");
var data = new Object();
form.append($(":checked").clone());
$.each($("select"), function(i, item) {
if ($(item).val() != null) {
form.append(($(item).clone()));
}
});
$.each($("input[type='text']"), function(i, item) {
if ($(item).val().length > 0) form.append(($(item).clone()));
});
$("#allVendorInfo").val(form.serialize());
}).first().trigger("change");​

What the script to change the password should be in php?

<form action="final_change_pwd.php" method="post">
<table cellpadding="5" cellspacing="20">
<tr>
<td> Current Password: </td>
<td> <input type="text" name="txtoldpwd" /> </td>
</tr>
<tr>
<td> New Password: </td>
<td> <input type="text" name="txtnewpwd" /> </td>
</tr>
<tr>
<td> Confirm Password: </td>
<td> <input type="text" name="txtnewpwd" /> </td>
</tr>
<tr>
<td colspan="2"> <input type="submit" name="submit" value="Change Password" /> </td>
</tr>
</table>
</form>
// This is the form I have and I need a script to figure out what the process of changing password should be...!
// Anyone there to help me???
Fix the name of the input field for Confirm Password as it is the same as New Password. Also send the user ID along in the form as a hidden field.
Process would be to:
check the userid and current password. if they match:
check if new password matches the conformation.
then save it to db.
the process itself is not the hard part though, securing everything is. try to encrypt values before you post it (javascripts available that do this), and salt them afterwards. check if the new password meets possible password requirements (also javascript, before you post it) like at least 6 chars long, has a number in it etc, and even checking if the new password and conformation match can be done before you post anything using javascript.
As advice though, maybe you should consider not doing this yourself at all, because it will never be as secure as it should be.
There are plenty of resources online like this site that can walk you through what you need to do: http://www.plus2net.com/php_tutorial/php_change_password.php

Categories