SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    May 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can I force a form's action without a 'submit'?

    Let's say I have two forms on the same page. The first form is for input of text data and the second for is for uploading an image.


    1. I want the first form to record the input from the input fields to a mySQL table BEORE the action of the second form takes place.

    2. Then I want to upload the image file (which has already been selected by the "Browse" button) without the user having to click on an "upload image" button. I need to force the "action" somehow after the action of the first form is complete.

    Here are the 2 forms, each with different actions:

    Code HTML4Strict:
    <form action="addItem.php" method="post">
    	<p>Item id: <input type="text" name="item_num" size="15" maxlength="20" value="<?php if (isset($_POST['item_num'])) echo $_POST['item_num']; ?>" /></p>
    	<p>Item Name: <input type="text" name="item_name" size="15" maxlength="40" value="<?php if (isset($_POST['item_name'])) echo $_POST['item_name']; ?>" /></p>
    	<p>Item Type: <input type="text" name="item_cat" size="20" maxlength="80" value="<?php if (isset($_POST['item_cat'])) echo $_POST['item_cat']; ?>"  /> </p>
     
    	<p>Item Description:<br/><textarea name="item_desc" id="textarea" cols="35" rows="5"></textarea>
     
     
    	<p>Item Price: <input type="text" name="item_price" size="20" maxlength="80" value="<?php if (isset($_POST['item_price'])) echo $_POST['item_price']; ?>"  /> </p>
        <br />
        <br />
        <label>
        <input name="onweb" type="checkbox" checked ="checked" />
        show this item on the website</label>
        <br />
      </p>
    	<input type="hidden" name="submitted" value="TRUE" />
    	<p><input type="submit" name="submit" value="Post Item" /></p>
    	<input type="hidden" name="submitted" value="TRUE" />
    </form>
     
     
    <form action="processImage.php" enctype="multipart/form-data" method="post">
    <tr>
        <td valign=top><strong>Image file:</strong></td>
        <td><input name="upfile" type="file" size="30"><br>
            Image files must be in JPEG, GIF, or PNG format.
        </td>
    </tr>
     
    <tr>
        <td colspan="2"><br/>
    	<input type="submit" value="Upload Image">
        </td>
    </tr>
    </form>

    I hope this is stated clearly. Thanks again for any assistance.

    Chop

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, you cannot do this with HTML or XHTML. They are markup languages whose only purpose is to indicate the semantic meaning of the content.

    You could achieve something like this with ajax, but that question belongs in the JavaScript forum.
    Birnam wood is come to Dunsinane

  3. #3
    Resident curmudgeon bronze trophy gary.turner's Avatar
    Join Date
    Jan 2009
    Location
    Dallas
    Posts
    990
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I may be missing the point, but I don't see any reason to use two forms. Submit everything, and process in any order you prefer.

    cheers,

    gary
    Anyone can build a usable website. It takes a graphic
    designer to make it slow, confusing, and painful to use.

    Simple minded html & css demos and tutorials

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by gary.turner View Post
    I may be missing the point, but I don't see any reason to use two forms.
    The only reason why you'd use two forms is if the they can de used independently - some people use form one and others use form two and some use one and then come back to use the other.

    I agree with you that for what the OP is trying to do there should only be one form.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I couldn't guess your intention in making two forms. Capture the first part even if the second part didn't work?
    The page below will be helpful:

    How to create a multi-submit form

  6. #6
    SitePoint Member
    Join Date
    May 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess my thinking was that using a form that begins with:
    form action="processImage.php" enctype="multipart/form-data" method="post">

    it was only for uploading an image. Are you saying I can add any other input fields that I want? (still new to forms)

    I'm using php with the form to upload some text information into a database table then, once the data has been added, retrieve the unique primary key number from that record so that I can append it onto the front of the image name that will be uploaded in order to make sure the image name itself is unique. For instance:

    myImage.jpg ----> 102myImage.jpg

    where the 102 is the same as the primary key for that record.

    Therefore I need the text data uploaded before the actual image is uploaded to have time to retrieve the unique number. However, I still wanted the user to be able to browse to the image that will be uploaded on the same form as all the other input.

    ...hope this makes sense. There are better ways maybe, so any suggestions are welcome.

    thanks

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    multipart/form-data means that the form consists of multiple parts. One part will be the file being updated via the <input type="file"> and the other part will be all the other fields input into the form.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Member
    Join Date
    May 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Felgall. I put everything into one form, shuffled things around for about an hour, sprinkled a little php here and there and ... voila, seems to be doing about what I wanted it to do. A little more fine tuning and I'll be ready for evening cocktails.


    chop

  9. #9
    SitePoint Member
    Join Date
    May 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Steve, rather


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •