UPDATE using html form (edit button) php/mysql - php

I'm trying to figure out how to UPDATE a post in my db using html form. I have a page with a form using INSERT that successfully uploads data to my db. I also have another page presenting that data using SELECT. On this page I would like to have an Edit button, sending me to a similar page with the original form but this with the data from the db already inserted into the form, for easy editing and updating. Basically I don't know where to start and I need your help! It's an album database btw.
form page
<form method="post" action="spara2.php" name="std">
<label>Cover:</label><br>
<input id="file" type="file" name="file"><br>
<label>Title:</label><br>
<input type="text" name="std_title" size="58"/><br>
<label>Artist:</label><br>
<input type="text" name="std_artist" size="58"/><br>
<label for="select"><br>
Year:</label><br>
<select name="std_year">
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
</select><br>
<label>Description:</label><br>
<textarea name="std_beskriv" cols="45" rows="7">
</textarea><br>
<label>Tracks:</label><br>
<textarea name="std_lista" cols="45" rows="7">
</textarea><br>
<label>Spotify HTTP Link:</label><br>
<input type="text" name="std_spotify" size="58"/><br>
<br>
<label for="reset"></label><input type="reset" value="Reset"/>
<label for="save"></label><input type="submit" name="std_save" value="Save"/>
</form>
My db is called album and the table musik. I want a similar page but with the data from the db inserted into it. My table has:
id
title
artist
year
beskriv
tracks
spotify
date
Since there's a lot of posts in the db how can I select specific entry's so that I can edit one album at a time?
Hope you can understand what i'm after.
Thanks!

You should use $_GET to get a specific album from the DB, and use the attributes as default values in the form. Here's an example.
Album1 Edit
Album2 Edit
Album3 Edit
Album4 Edit
Album5 Edit
Album6 Edit
When you retrieve all the albums from db, you should use the unique id of each album to create a unique path to update a specific album. So the Edit button should have a link of this form:
<a href="edit.php?albumID=<?php echo $album[id];?>">
So after writing this, you will have for the first album edit link: edit.php?albumID=1
To create the edit.php page, first you should fetch from the DB the album by specifiying the ID passed by the $_GET['edit'] parameter in the url. So this will look like:
$albumid = mysql_real_escape_string($_GET['edit']);
$sql = "SELECT * FROM musik where albumid = $albumid";
If the record was not found, redirect to a 404 page. Otherwise, echo the attributes in the corresponding inputs.
Ex:
<input type="text" name="std_artist" value="<?php echo $album['std_artist'];?>" size="58"/>

Related

How to handle an HTML form containing dynamic muliselect fields using PHP?

I'm chasing a bit of a challenge handling an HTML form whose input fields are created dynamically using JavaScript DOM containing multiple select fields as well. Here is a sample code of the form I have:
<input type="text" name="attributeIndex[]" id="brand" class="form-control" placeholder="brand">
<select name="attributeIndex[]" id="color" class="form-control">
<option value="92">Aero</option>
<option value="277">Aluminum</option>
<option value="91">Amber</option>
</select>
After submitting the form, I want to get the form data in a PHP array like this:
$formData=$_GET['attributeIndex'];
The array $formData should be like this:
array(
[0]=>text input value here
[1]=>array(list of selected colors here)
)
Any suggestion will be highly appreciated!
Name the select with an additional []:
<select name="attributeIndex[][]"
That way each value PHP puts into $_GET will be in an array in an array.
You'll also need to add the multiple attribute if you want more than one option to be selected.
<select name="attributeIndex[][]" multiple id="color" class="form-control">
<form action="#" method="post"><input type="text" name="attributeIndex['brand'][]" id="brand" class="form-control" placeholder="brand">
<select name="attributeIndex['color'][]" id="color" class="form-control" multiple="multiple">
<option value="92">Aero</option>
<option value="277">Aluminum</option>
<option value="91">Amber</option>
</select>
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST['submit'])) {
print_r($_POST['attributeIndex']);
}
?>
You can try above this. By using attributeIndex['brand'] you can get dynamic brand names and attributeIndex['color'] you can get dynamic color names.

Sending value of a select tag to a PHP page as a POST variable

I have a select tag with different values as shown below.
I want to send the selected value to a PHP page as a POST variable or any other way.
Is this possible?
<select id="city" name="city" align="left">
<option value="Pune">Pune</option>
<option value="Bhopal">Bhopal</option>
<option value="Mumbai">Mumbai</option>
<option value="New Delhi">New Delhi</option>
</select>
Put your select inside a form. Give the form an action that is a php file. give it the method of post and include a <input type="submit" value="Submit"> inside the form as well.
Then, in your php file you will access the $_POST array
echo $_POST['city'];
If you'd like to submit it without the button, you'll need to use javascipt's onChange event.
Yes, and it's very simple. You have to use the from tag : http://www.w3schools.com/html/html_forms.asp and learn a little of PHP and HTML basic interaction
<form action = 'myPage.php' method = 'post'>
<select id="city" name="city" align="left">
<option value="Pune">Pune</option>
<option value="Bhopal">Bhopal</option>
<option value="Mumbai">Mumbai</option>
<option value="New Delhi">New Delhi</option>
</select>
<input type = 'submit' name = 'send' value = 'send'/>
</form>
(I also added a button son send data)
in myPage.php :
<?php
if(isset($_POST['city'])){
echo isset($_POST['city'];
}
?>

Populate inputs on selects JQUERY

i have a dropdown menu generated by my php when the page loads filling with my rows in the sql table, being the table ID(Value) and they NAME.
<select>
<option value="0"></option>
<option value=6>Alientech</option>
<option value=2>FNAC</option>
<option value=5>Logitech</option>
<option value=1>MHR</option>
</select>
Then i have:
<input type="text" name="editname">
<input type="text" name="editmail">
<input type="text" name="editwebsite">
<input type="text" name="editphone">
<input type="text" name="editfax">
<input type="text" name="editadress">
<input type="text" name="editincharge">
What i want to do is on selecting the option above he gets the id of the row in mysql and fills the inputs accordingly to the values in the SQL table so a person can edit the values on submitting the form.
How can i with jQuery do this?
Where is your code attempt?
The way to do this is on select of the jquery
you would take the value of the select where
<select id="some_id">
also you will need to ID all your fields of input.
Your PHP script should return a JSON where your on success you parse the JSON and set it to the value of your INPUT field via ID.
$.ajax({
url: some_php_mysql.php
type:post,
data: {some_id:$('#some_id').val()},
success:function(data){
//json object returned - parse and then ID each value
});
First, you could reload the page :
<form action="" method="post" id="myform">
<select name="myselect">
<option value="0" disabled="disabled"></option>
<option value=6>Alientech</option>
<option value=2>FNAC</option>
<option value=5>Logitech</option>
<option value=1>MHR</option>
</select>
</form>
With a little bit of jQuery...
$("form#myform select").change(function(){
$("form#myform").submit(); // Submit the form if the value is changed.
});
And when you load your page, if you find a value for $_POST["myselect"] then you have to load something in the fields (except if it's 0).
You could also do things asynchronously with Ajax (instead of submitting the form, send a HTTP request and parses the result to fill the form). However, if the purpose of your page is only to edit an entry, the you should use the above method.
Ajax reference : http://www.w3schools.com/ajax/

Posting info on a page depending on value chosen in selectbox

I am working with creating an ad posting page. I have included dependable select boxes as the category selection system. All the values are being pulled from MySQL database. The table is composed of an id (auto increment) bridged by master_id and identified by category name. Every category has a subcategory. So the user is forced to pick a subcategory before being able to submit the info. I am currently looking for ideas of how to post info that has been input by user depending on the category chosen. I have created an individual page for each subcategory. So every time an input is submitted, I want it to display in the page accordingly. For example, if I pick category: Books and subcategory:Textbooks then post it in textbooks.php. Live EXAMPLE
Table Structure
Drop down menus
<form action="" method="post">
<select name="category" id="category" size="7" class="updateCategory" >
<option selected="selected" value="">Select one:</option>
<?php if (!empty($list)) { ?>
<?php foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>">
<?php echo $row['name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
<select name="subc1" id="subc1" size="7" class="updateCategory"
disabled="disabled" hidden="hidden" >
<option value="">----</option>
</select>
<select name="subc2" id="subc2" size="7" class="updateCategory"
disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc3" id="subc3" size="7" class="updateCategory"
disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
</br>
Insert your name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>
This is a little bit of a shot in the dark, as the question was vaguely worded.
I think that, first, you will need to make an update to the database. You will need to create either a new table or a new column. I would personally choose the table as that provides more flexibility, like so: id, category_id, category_page (and you could put other columns in too). Obviously, category_id references back to the main table category.
Then, do a select like this:
SELECT [COLUMNS HERE] FROM category RIGHT JOIN category_page ON category_page.category_id = category.id
You now have the values you need.
Then, in your PHP, return the url to the page (if you are using JS to dynamically show the new page):
<option value="xyz" data-url="url-for-page.php">Category Name</option>

HTML form submit to PHP script

I am making an HTML form. I want the results to appear in the PHP script.
<form action="chk_kw.php" method="post"> <br />
<select> name="website_string"
<option value="" selected="selected"></option>
<option VALUE="abc"> ABC</option>
<option VALUE="def"> def</option>
<option VALUE="hij"> hij/option>
</select>
<input type="submit" name="website_string" >
</form>
The problem is I cannot get the value passed to the PHP. if I use value:
<INPUT TYPE="submit" name="website_string" value="selected" >
It always passes the text in the quotes. In this case "selected". How do I pass one of the strings from the option?
Try this:
<form method="post" action="check.php">
<select name="website_string">
<option value="" selected="selected"></option>
<option VALUE="abc"> ABC</option>
<option VALUE="def"> def</option>
<option VALUE="hij"> hij</option>
</select>
<input TYPE="submit" name="submit" />
</form>
Both your select control and your submit button had the same name attribute, so the last one used was the submit button when you clicked it. All other syntax errors aside.
check.php
<?php
echo $_POST['website_string'];
?>
Obligatory disclaimer about using raw $_POST data. Sanitize anything you'll actually be using in application logic.
<form method="POST" action="chk_kw.php">
<select name="website_string">
<option selected="selected"></option>
<option value="abc">abc</option>
<option value="def">def</option>
<option value="hij">hij</option>
</select>
<input type="submit">
</form>
As your form gets more complex, you
can a quick check at top of your php
script using print_r($_POST);,
it'll show what's being submitted an the respective element name.
To get the submitted value of the element in question do:
$website_string = $_POST['website_string'];
It appears that in PHP you are obtaining the value of the submit button, not the select input. If you are using GET you will want to use $_GET['website_string'] or POST would be $_POST['website_string'].
You will probably want the following HTML:
<select name="website_string">
<option value="" selected="selected"></option>
<option value="abc">ABC</option>
<option value="def">def</option>
<option value="hij">hij</option>
</select>
<input type="submit" />
With some PHP that looks like this:
<?php
$website_string = $_POST['website_string']; // or $_GET['website_string'];
?>
Assuming you've fixed the syntax errors (you've closed the select box before the name attribute), you're using the same name for the select box as the submit button. Give the select box a different name.
For your actual form, if you were to just post the results to your same page, it should probably work out all right. Try something like:
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="POST>
Here is what I find works
Set a form name
Use a default select option, for example...
<option value="-1" selected>Please Select</option>
So that if the form is submitted, use of JavaScript to halt the submission process can be implemented and verified at the server too.
Try to use HTML5 attributes now they are supported.
This input
<input type="submit">
should be
<input name="Submit" type="submit" value="Submit">
whenever I use a form that fails, it is a failure due to the difference in calling the button name submit and name as Submit.
You should also set your enctype attribute for your form as forms fail on my web host if it's not set.

Categories