SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Preview page from form

    Hello all,

    I searched the forums but did not find an answer to my particular question. I have a form which when a user clicks on the preview button, he/she sees how it will look published on his/her website.

    When they click submit I need the form to submit as normal. I am trying the following but I can not get my preview variable to equal 1.

    //My logic for deciding which <form> to use

    Code:
    <script type="text/javascript">
    	var preview;
    
    	if (preview ==1) {
    	document.write("<form method='post' name='storySubmit'>") } else {
    		document.write("<form method='post' name='preview' action='storyPreview.php' target='_blank'>")
    		}
    </script>
    //The preview and submit button
    HTML Code:
    <!--The Preview Button --> <input type="submit" value="Preview">
    <!--The submit button -->	
    <a href="javascript:toggleLayer('commentForm');" title="Add a comment to this entry" onClick="alert(setVar())";>
    Are you ready to submit your story?
    </a>
    <div id="commentForm">
    <input type="submit" value="Submit"/>
    //This is how I set preview=1

    Code:
    function setVar() {
    	var preview =1;
    	return preview;
    When I use the submit button, even though I can confirm via the alert that preview is equal to 1, I always go to the preview page, storyPreview.php. Where am I going wrong?

    Thank you all

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    some progress

    Hi everyone,

    This has not been a popular thread it seems but I have made progress on my own. I realized that calling a function would consistently not allow me to store the state of the variable. Duh! CS 101.

    This is now in my script tag within the head of my document.
    Code:
    	var preview;
    		
    	function isSet() {
    		if (preview == 1) {
    		set =1;
    		return set;
    		} else {	
    			set =0;
    			return set;
    		}
    	}
    My preview and submit button are wrapped in if else statements.
    Code:
    <script type="text/javascript">
    
    	if (isSet() !=1) {
    	document.write("<input type='submit' value='Preview'/>") } else {
    			document.write("Please submit your article.");
    		}
    </script>	
    
    <script type="text/javascript">
    	if (isSet() == 1) {
    	document.write("<input type='submit' value='Submit'/>") } else {
    			document.write("Please review your work.");
    		}
    </script>
    The key is a link that makes my submit button appear to the user, where I assign the variable
    Code:
    preview=1;
    HTML Code:
    <a href="javascript:toggleLayer('commentForm');" title="Add a comment to this entry" onClick="preview = 1;">
    Are you ready to submit your story?
    </a>
    Why is this not working? Any hints at all.

    Thank you.

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a bit confused. Is this the sort of thing you are after?
    Code:
    <form method="post" action="storySubmit.php">
    	<input type="text" name="someField"><br>
    	<input type="submit" value="Submit Story">
    	<input type="submit" value="Preview Story" onclick="this.form.action='storyPreview.php';">
    </form>

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    :) Rofl

    Wow, I guess I was over thinking that one. Talk about Mondays. Thank you very much. That worked great. 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
  •