SitePoint Sponsor

User Tag List

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

    XML Serialization Posting To Same Page

    Been pulling my hair out on this one. Did some searching on the forum, but did not find anything resembling my question. Then again, I am so new I could look at an answer and probably not even know it....

    Sorry for the long post.

    I am very new to PHP, but I have managed to create a form, that when submitted, serializes the forms fields into valid XML using the PEAR::HTML_Serialize package and saves the file on my local system. In order for this to work, the form needs to be submitted to another PHP page via the action attribute on the form element. So far so good, nothing difficult.

    Now, I would like to post the form to itself so I can validate on the client side before submitting the form and saving it to my local system. Everything works fine, except every time I reload the page, the PHP code is run, generating a new XML file even if there is nothing in the form fields. I will end up getting a huge XML file with nothing in it if people just refresh the page and bypass the submit button associated with the validation code.

    I am thinking I should put the serialization code into a function and call that function via a javascript event handler on a submit element, but I am not sure if that would work or even be possible.

    So basically, my question is, how can I still accomplish the serialization without generating a new XML file every time the browser gets refreshed and only let it happen on an actual submission?

    I can post the hideous code that actually works if anyone is brave enough to address this rambling.

    Thanks in advance.

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Firstly, add a hidden field which you'd check for and only then process the form, ex.:
    Code php:
    echo '<form method="post">
    ...
    <input type="hidden" name="posted" value="1" />
    ...
    ';
     
    if(isset($_POST['posted'])){
      //process the form
    }

    And secondly once you process the form, add a redirect to the same page or any page:
    Code php:
    header('Location: http://example.com/page.php');
    Saul

  3. #3
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the response.

    I guess I am too daft to understand how this would help. I understand that hidden fields can hold information that is not visible on the actual page, but how is checking the value of this field going to ensure that a page reload does not run the PHP serialization code that is on the same page? If the value is always true, then wouldn't I be in the same situation?

    Do I need to move my PHP code to another page? If so, I tried this with an include(); statement and the code just gets run every time there is a page refresh.

    Sorry for being so clueless on this and thanks again for the response.

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Create you a form_handler page to do the serialization work, then use header() to return to the form page. You can do form checking with php, but it is much easier to use javascript to check the form before posting.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you wrap your serialization code into if(isset($_POST['posted'])), as per example, the code will be executed only when the form is submittet. Otherwise $_POST['posted'] will not be set.
    Saul

  6. #6
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thank you both for your help in figuring this problem out.

    I ended up using the header() function on the form page and a subsequent PHP handlers page to accomplish the serialization with an onsubmit() event handler in the form element.

    By disabling the submit button if a file name is not provided prior to the serialization (the serialization code also saves the output), the code will not run during a random page refresh from the browser giving me a blank XML file.

    This is really appreciated since the next step is utilizing a native xml database to play with the generated xml file.

    Thanks again.


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
  •