Jump to content

Advice on PHP Form for classifieds site


singy

Recommended Posts

Hi,

 

Im just after a bit advice on the best strategy to create a form for a classifieds website im working.

 

The form will be used to create a new advert record and will contain various information, for example advert title, location, description, price etc....  I also want the user to be able to upload multi images on the form at the bottom. 

 

Im just trying to work out the best way to do this.  The mysql table for the advert would have an advert id which is auto incremented and I would also have an images table which includes the advert ID so I can have multiple images for each advert and save the path details of the images in the images table.

 

The plan on the advert form was to have the advert details at the top and then a section where I could upload images one at a time near the bottom using a seperate image upload button, which would upload the images to the servers file system.  Each time I upload an image I plan to refresh the form with the existing data ive entered and display thumbails of the images ive uploaded.  Each image thumbail would ahve a deleted option next to  it, so I can delete them if necessary. I would then have a save advert button which would insert the advert details in to the mysql table.

 

The problem is, on the advert form, if I enter several fields, then upload an image before I save the advert, I wont know the advert ID to insert the image in the image table as it wont have been generated yet.  Should I seperate the form into a multiple form so I can input the advert details on form one and then upload the images on form 2 after the record has been created ???  Or maybe insert the record as soon as I open the form so I have the advert ID from the start?

 

Im just a bit confused on how to go about arranging the form etc...  Surely there are many people who have created a form to input data and also upload multiple images to be associtated with the record.  If anyone has done this before, which way have you gone about it?  I would be very interested to hear.

 

Many Thanks for your time

Mark

 

 

Link to comment
Share on other sites

I'm only going to address the issue you present regarding the upload of images before form submission. This is how I would approach that problem.

 

I woulld have two forms on the page. The main form would be for the advert details and the second form at the bottom would be for uploading images. When the page is first displayed I would use some randomly generated value that is inserted into hidden fields in the forms when the page is created by PHP.

 

Then I would use AJAX when using the form to upload images. The AJAX call would submit the data (i.e. File) and return back a path to the image so you can use JavaScript to display the image without reloading the page. When saving the file details to the database you would use the randomly generated value to identify those images.

 

Then when the user finally submits the advert the process to add the advert to the DB would get the ID of the new advert record and then perform a query on the images table to change the random code to the actual ID.

 

(Actually I'd probably use a separate table for the temp images until the advert is finalized)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.