I have a list of email addresses in a table, which is populated via an SQL query. Next to each email address, I've placed a submit button which I want to use to delete the email address that appears in that particular table row.
I thought of appending the email address to the name of each delete button, with the hope that it will take me in the right direction.
while($row = mysql_fetch_array($result))
echo "<tr><td>".$row['email_adress']."</td><td><input type=\"submit\" value=\"Delete\" name=\"delete".$row['email_adress']."\"></td></tr>";
I'm wondering how to use the delete button for each entry. Any help?
I think the issue is that you are attempting to use submit buttons to contain data when submit buttons are not meant to contain data. Well they can and people do use them that way, but I prefer to use submit buttons to determine how to handle the data rather than to be the data in the submit. There are two basic methods that I think would be a fair extension to what you are trying to do.
1.) Use links to issue the delete
2.) Use the submit button to issue the delete, but include a hidden form field to contain the email address to delete.
In the case of using a link you don't have to deal with a form, but I don't know about the rest of the page. If you are sending any other data, then a form is the ideal way. If you are just sending a single piece of data to delete the email, then I suggest a link. My recommendation come from a functional perspective. You might have a UI reason to a button that I don't know about.
In the case of using a form, the hidden field should contain the primary key (untested):
while($row = mysql_fetch_array($result)) {
echo "<tr>
<td>".$row['email_adress']."</td>
<td><form action="some/uri/to/something">
<input type=\"hidden\"
name=\"emailAddress\" value=\"{$row['email_adress']}\"/>
<input type=\"submit\" value=\"Delete\"
name=\"delete".{$row['email_adress']}."\">";
</form>
</tr>
</td>";
}
You have to create a form around each submit else every hidden field in the form would be submitted telling you nothing about which email to delete. You can do many more things with the submit by using Javascript, but the simple method would be to just use a link -- forget about forms.
If a button is absolutely a must, you could output a small form for each button and add a hidden field containing the id (or email, if you insist). The form would submit the id or address in the hidden field, which you pick up in your processing script and run your delete query.
edited to remove a portion of the answer that was determined to be an unwise method
so I've been messing around with this code trying to get a "prefetch browser add-on" to trigger something and kill my database... but I havn't been able to trigger anything yet... but I'd sure like to.
<?php
//CREATE AND POPULATE DB
if(!file_exists('a.sqlite')){
$db = new SQLite3('a.sqlite');
$db -> exec("CREATE TABLE test (idx INTEGER PRIMARY KEY, number INTEGER NOT NULL);");
for($i=0;$i<9;$i++){
$db -> exec("INSERT INTO test (number) VALUES (".(rand(1000,9999)).");");
}
}else{
$db = new SQLite3('a.sqlite');
}
//PERFORM DELETE FROM HREF
if(isset($_REQUEST['delete'])){
$db -> exec("DELETE FROM test WHERE idx = ".$_REQUEST['delete'].";");
}
//SHOW CONTENTS OF BASE OR DELETE DB FILE
$result = $db->query('SELECT * FROM test');
while ($row = $result->fetchArray()) {
echo "<a href='test.php?delete=".$row['idx']."'>".$row['number']."</a></br>";
}
?>
Yes, you could do it like this but that would require to either execute the query again on the form's target page and then check for every combination of "delete".$row['email_adress'] or use a construction like this:
foreach ($_REQUEST as $key => $dummy)
if (substr($key,0,6) == 'delete')
delete_entry(substr($key,6));
However, if you simply swap value and name...
<input type=\"submit\" name=\"Delete\" value=\"".$row['email_adress']."\">
...you can just check $_REQUEST['Delete'] to find out which address button has been clicked.
... I'm still not sure about displaying peoples emails on web pages unless it's company internal.. I gather we are in a secure company back office?
I'd go for the:
<a href=my_page.php?delete_email_id=456>email</a>
probably with some annoying javascript for the confirm
email
and catch it with a
if(isset($_REQUEST['delete_email_id']){ SQL UPDATE ENTRY...}
at the top of my_page.php
Related
I have a text box which extracts the content of dropdownlist.Now whenever i extract the content i too need to edit it ana save it into the database.How can i do????
Here is my code:
<?php
require'conn.php';
$select_query="Select dynamictext from tbl_content where type=1";
$select_query_run =mysql_query($select_query);
echo'Dynamictext:';
echo "<select name='dynamic text' id='names' >";
while ($select_query_array= mysql_fetch_array($select_query_run) )
{
$value=$select_query_array["dynamictext"];
echo "<option value='$value' >".htmlspecialchars($select_query_array["dynamictext"])."</option>";
}
echo "</select>";
?>
Based on the clarification I got above from #krisha above, I'm going to take a stab at answering this. You'll want to refer to my comment above, for a definition of (Option A) and (Option B), as I defined them.
Let's assume you've got (Option A) working and that (as far as the select HTML element is concerned), it is functional.
Let's also assume that you know to do the following:
Place the select tag inside of a form tag.
Set the form tag's action and method values.
Place a <input type="submit" value="Submit"> inside of the form tag.
If none of the above made sense, see here.
Once you've done everything above, that will result in the value of the HTML drop-down being available to PHP after the user clicks the Submit button and the page refreshes. How the value of the drop-down is passed through the submit process will depend on the method value you pass to the form tag. I'll assume you use method="get" (which will result in the value of the drop-down appearing in the URL after the refresh). If you want more info on get versus post, see here.
Once the refresh occurs, you use PHP's $_GET[""] to retrieve the value of the HTML drop-down. In your case, you would use $_GET["dynamic text"] (since the name of your select is dynamic text). You could set this value to a variable, like so:
$value_of_select = $_GET["dynamic text"];
At this point, you have the value the user selected from the HTML drop-down. Now, push it to the database. It looks like you already understand how to pass queries to a database. The only difference in this case is that you want to do an insert or an update, not a select.
I hope I'm not posting a duplicate question but I've looked around (and googled as well!) and nothing has given me the answer I'm looking for.
I have a form in HTML. When the user submits the form the values get stored with mysql under their user account for the site.
The issue is, I'd like the user to be able to go back and edit the form any time they like.
I could certainly just populate the form with values from php when the users review the form, but it gets tricky when I try to populate a file input field (and the file has been saved in mysql using the blob type). Not to mention that I'd like to do this as cleanly as possible.
Ideally it would be nice if there was a convenient module for reviewing forms that have already been submitted in JQuery per se.
Can anyone offer any advice? Thanks in advance!
Edit:
Here's a good example of what I mean - in chrome if I fill out a form and redirect to the next page after hitting submit, if I hit back I come back to the form and it's still filled out with the information I entered previously! Could I invoke this behaviour whenever I want to, as opposed to only when the user hits back?
You can't pre-fil an <input type="file" . . but surely when they come back to the form, they want to see the file they've uploaded .. this is what you mean right ..
So if its a picture, you could just do: <img src="loadpic.php?id=$var" />
If it's files they've uploaded, just list the file name / date and other data.. etc in some sort of list.
Then you could still show the <input type="file"> .. but with the label, 'add more pictures' or 'add another file'. .etc
Unless someone has a better way, at the moment I'm using a combination of 2 things:
1) Utilizing the $_SESSION variable
2) Setting the "name" attribute of every input in the form to the name of the field it corresponds to in the database.
This way I can loop through all the values dynamically instead of hardcoding them all in. Some input types (like file) are exceptional and will be handled on their own. Other that I can do something like this:
To insert into mysql:
$fields = array();
$values = array();
foreach ($_POST as $field => $value) {
$fields[] = $field;
$values[] = addslashes($value);
}
$fieldString = 'Table_Name('.implode(', ', $aFields).')';
$valueString = "VALUES('".implode("', '", $aValues)."')";
mysql_query("INSERT INTO $fieldString $valueString");
Reviewing the form is somewhat similar. I am using javascript to hook into document.onload. I need to pass javascript the records from mysql so that it may populate the form. Then it's a simple matter of getting elements by their name and assigning them their values that were passed from php.
The easiest way to do it and not have to go back to the database would be to store the values in a session.
<?php $_SESSION['myvalue'] = $inputvalue; ?>
On the html form use:
<input type="text" name="myName" value="<?php echo $_SESSION['inputvalue']; ?>" />
When completed don't forget to unset the session variable:
<?php session_start(); unset($_SESSION['myvalue']); ?>
I've been working on a way to build an archive for new threads. The over all goal was to make it so that if someone wanted to edit or delete a news thread they could, as well they could save a thread as a draft so that it ain't displayed to the public. I am using MySQL to store all the news threads, and I have it so that it prints out every news feed and the information for it. But when i click the edit button to edit that thread, it ALWAYS uses the id for the last MySQL entry called and NOT the ID I set it to use via a hidden form. Anyways here's the code and all parts to it. I'm so confused, and could really use some help. If you got questions just ask.
Main Script: http://pastebin.com/hn3cgVXu
Article_Post: http://pastebin.com/hhaLkuXe
Article_Archive: http://pastebin.com/X2fDg4dk
The original value for ID is called from the database, and set from article_archive
Display:
http://i25.photobucket.com/albums/c51/dog199200/Untitled-2.png
The Pencil is Edit, Trash Can is Delete. The image clearly shows that the loop is getting the ID, but that specific ID isn't being passed when the edit image is clicked.
In your Article_Archive when you loop through your database results you are naming your hidden input field the same thing for all the results.
<?php
while($row = mysql_fetch_array($news_list)) {
echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\" id=\"result_".$row['id']."\" name=\"result_".$row['id']."\">";
// ...
echo "... <input type=\"hidden\" name=\"id\" value=\"".$row['id']."\">";
// ...
echo "</form>";
} ?>
You're calling it id, so when you place multiple hidden input fields on the same form it will just grab the last one. Where is the javascript for when you click edit? You won't be able to do a standard form submit with that code since you're overwriting all the input fields with the same name attribute.
I'm running into a problem with changing the name attribute on an input element when a button is clicked.
I have 3 buttons:
* Add Renter
* Delete Customer
* Update Customer
This is the input element:
<input type=\"checkbox\" name=\"todelete[]\" value=\"$busRow[0]\" class=\"check\" />
$busRow is the user's id..
The form is submitted to a php script that checks to see what button was set and does different actions for that button. When the user clicks the delete it loops through and deletes each customer that was in the todelete[] array. When the user clicks the Update Customer button i want to change the checkbox input's name to "id". Then my php script will grab the user's id instead of grabbing the array value and then redirect the user to the Update Customer page with the id stored on the URL.
This is the javascript i'm using at the bottom of the page:
<script type="text/javascript">
$("button").click(function () {
if ($(this).text() == "Update Customer") {
$('input.check').attr("name", "id");
}
});
</script>
When i get to the Update Customer page the id is not there in the URL and i can't pull any values based upon the ID :(
You're setting the name value to "id", instead of the value stored in value. Try this:
$("button").click(function () {
if ($(this).text() == "Update Customer") {
var element = $('input.check');
element.attr("name", element.attr("value"));
}
});
Although, I think you could just simply pass the value of the input tag instead of the name when the btn clicked is 'Update'. That would save you the hassle of changing these different attributes and could save you trouble.
Edit: I'm reading the OP again and see you want to set the name att to "id" and not the user's id??? You're question is vague and hard to understand what you're trying to do. You're leaving out crucial code that grabs this "id" and redirects the user.
Edit2: What's up with the semi-colon between your string concat? I hope that's not in your source code. Now seeing your redirect code. I think your problem lies in trying to change the tag's attributes/values with an 'input.check' selector. I would also say you should redesign the way you are implementing this. Your checkboxes shouldn't hold your user information. But regardless, I don't think you need to even change the name value. If update isset() then just make $id = (its current name value); Download firebug to debug your javascript code. Firebug will also show you errors in your code that wouldn't see otherwise. It should help you out a lot
Try putting an alert inside the if statement to check if that is actually becoming true, to narrow down what might be the cause.
Also I've never seen input.check used as a selector. Perhaps give the button an id making it:
<input id=\"customercheck\" type=\"checkbox\" name=\"todelete[]\" value=\"$busRow[0]\" class=\"check\" />
Then change the attribute via the ID like so:
$('#customercheck').attr("name", "id");
that also saves you if you add another input to the field later down the road.
I have a database which holds the residents of each house in a certain street. I have a 'house view' php web page which can display an individual house and residents when given the house number using 'post'. I also have a 'street view' web page which gives a list of houses. What I want to know is if you can have links on the street view which will link to the house view and post the house number at the same time without setting up a form for each?
Regards
If you want to pass the data using POST instead of GET, you can do it using a combination of PHP and JavaScript, like this:
function formSubmit(house_number)
{
document.forms[0].house_number.value = house_number;
document.forms[0].submit();
}
Then in PHP you loop through the house-numbers, and create links to the JavaScript function, like this:
<form action="house.php" method="POST">
<input type="hidden" name="house_number" value="-1">
<?php
foreach ($houses as $id => name)
{
echo "$name\n";
}
?>
</form>
That way you just have one form whose hidden variable(s) get modified according to which link you click on. Then JavaScript submits the form.
I assume that each house is stored in its own table and has an 'id' field, e.g house id. So when you loop through the houses and display them, you could do something like this:
<a href="house.php?id=<?php echo $house_id;?>">
<?php echo $house_name;?>
</a>
Then in house.php, you would get the house id using $_GET['id'], validate it using is_numeric() and then display its info.
You cannot make POST HTTP Requests by some_script
Just open your house.php, find in it where you have $house = $_POST['houseVar'] and change it to:
isset($_POST['houseVar']) ? $house = $_POST['houseVar'] : $house = $_GET['houseVar']
And in the streeview.php make links like that:
Or something else. I just don't know your files and what inside it.
This is an old thread but just in case anyone does come across i think the most direct solution is to use CSS to make a traditional form look like an anchor-link.
#ben is correct you can use php and javascript to send a post with a link, but lets ask what the js does -- essentially it creates a form with style='display:none' sets an input/text line with value='something' and then submits it.
however you can skip all this by making a form. setting style='display:none' on the input/text lines (not the form itself as above) and then using CSS to make the button look like a normal link.
here is an example is i use:
in PHP Class,
public function styleButton($style,$text){
$html_str = "<form id='view_form' action='".$_SERVER['REQUEST_URI']."' method='post' >";
$html_str .= "<input style='display:none;' name='list_style' type='text' value='".$style."' >";
$html_str .= "<input id='view_button' type='submit' value='".$text."' >";
$html_str .= "</form>";
return $html_str;
}
Then in the CSS id="view_form" set "display:inline;"
and in the CSS id="view_button" set to something like: "background:none;border:none;color:#fff;cursor:pointer"
I would just use a value in the querystring to pass the required information to the next page.
We should make everything easier for everyone because you can simply combine JS to PHP
Combining PHP and JS is pretty easy.
$house_number = HOUSE_NUMBER;
echo "<script type='text/javascript'>document.forms[0].house_number.value = $house_number; document.forms[0].submit();</script>";
Or a somewhat safer way
$house_number = HOUSE_NUMBER;
echo "<script type='text/javascript'>document.forms[0].house_number.value = " . $house_number . "; document.forms[0].submit();</script>";
This post was helpful for my project hence I thought of sharing my experience as well.
The essential thing to note is that the POST request is possible only with a form.
I had a similar requirement as I was trying to render a page with ejs. I needed to render a navigation with a list of items that would essentially be hyperlinks and when user selects any one of them, the server responds with appropriate information.
so I basically created each of the navigation items as a form using a loop as follows:
<ul>
begin loop...
<li>
<form action="/" method="post">
<input type="hidden" name="country" value="India"/>
<button type="submit" name="button">India</button>
</form>
</li>
end loop.
</ul>
what it did is to create a form with hidden input with a value assigned same as the text on the button.
So the end user will see only text from the button and when clicked, will send a post request to the server.
Note that the value parameter of the input box and the Button text are exactly same and were values passed using ejs that I have not shown in this example above to keep the code simple.
here is a screen shot of the navigation...
enter image description here