Jump to content


Photo

submitting form problem..


  • Please log in to reply
30 replies to this topic

#1 john_6767

john_6767
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 04 August 2006 - 01:38 AM

I'm trying to submit a form in my php by using this javascript but it doesn't seem to be working, all that happens is the page appears to reload again leaving me blank fields. I cannot use the standard form action becasue i am using server side validation.. any suggestions? I've been trying different things for days with no result.

  	else {
print "<script language=\"JavaScript\">";
print "document.checkout_form.action='checkout_confirm.php'";
print "document.checkout_form.submit()";
print "</script>";
}


#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 04 August 2006 - 01:40 AM

I cannot use the standard form action becasue i am using server side validation


Why would that prevent you from using a the normal submit procedure?

#3 john_6767

john_6767
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 04 August 2006 - 01:42 AM

if i use an action the form is passed fine but the server side script on the page is bypassed..

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 August 2006 - 02:36 AM

Please post your whole script, commented to indicate where you're having the problems.

Ken

#5 john_6767

john_6767
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 04 August 2006 - 02:44 AM

ok heres the code

<?php
//WA eCart Include
require_once("WA_eCart/chandlery_PHP.php");
?>
<?php require_once("WA_ValidationToolkit/WAVT_Scripts_PHP.php"); ?>
<?php require_once("WA_ValidationToolkit/WAVT_ValidatedForm_PHP.php"); ?>
<?php
$chandlery->GetContent();
?>
<?php 
if (isset($_POST["Checkout"]))  {
  $WAFV_Redirect = "checkout.php";
  $_SESSION['WAVT_checkout'] = "";
  if ($WAFV_Redirect == "")  {
    $WAFV_Redirect = $_SERVER["SCRIPT_NAME"];
  }
  $WAFV_Errors = "";
  $WAFV_Errors .= WAValidateRQ(((isset($_POST["Name"]))?$_POST["Name"]:"") . "",true,1);
  $WAFV_Errors .= WAValidateEM(((isset($_POST["Email"]))?$_POST["Email"]:"") . "",true,2);
  $WAFV_Errors .= WAValidateCC(((isset($_POST["CreditCardNo"]))?$_POST["CreditCardNo"]:"") . "","4:51:52:53:54:55:34:37:30:36:38:",true,3);
  $WAFV_Errors .= WAValidateRQ(((isset($_POST["CreditCardType"]))?$_POST["CreditCardType"]:"") . "",true,4);

  if ($WAFV_Errors != "")  {
    PostResult($WAFV_Redirect,$WAFV_Errors,"checkout");
  }
  




//this is where it is not working for me...





else {
  	print "<script language=\"JavaScript\">";
	print "document.checkout_form.action='checkout_confirm.php'";
	print "document.checkout_form.submit()";
	print "</script>";
  }





//end of where its not working...





}
?>
<?php
// WA eCart Redirect
if ($chandlery->redirStr != "")     {
  header("Location: ".$chandlery->redirStr);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>Newcastle Rigging & Chandlery</title>

<?php 
$currentPageGroup = "images/nav/online_shop.gif";
?>
<?php
    if ($chandlery->DisplayInfo("Name") <> "") {
    } else {
        print "<script language=\"JavaScript\">";
		print "window.location = 'shopping_cart.php?Action=checkoutFailed' ";
		print "</script>";
    }
?>

<link href="styles/nrc.css" rel="stylesheet" type="text/css">
<script type="text/JavaScript">
<!--

function GP_AdvOpenWindow(theURL,winName,features,popWidth,popHeight,winAlign,ignorelink,alwaysOnTop,autoCloseTime,borderless) { //v2.0
  var leftPos=0,topPos=0,autoCloseTimeoutHandle, ontopIntervalHandle, w = 480, h = 340;  
  if (popWidth > 0) features += (features.length > 0 ? ',' : '') + 'width=' + popWidth;
  if (popHeight > 0) features += (features.length > 0 ? ',' : '') + 'height=' + popHeight;
  if (winAlign && winAlign != "" && popWidth > 0 && popHeight > 0) {
    if (document.all || document.layers || document.getElementById) {w = screen.availWidth; h = screen.availHeight;}
		if (winAlign.indexOf("center") != -1) {topPos = (h-popHeight)/2;leftPos = (w-popWidth)/2;}
		if (winAlign.indexOf("bottom") != -1) topPos = h-popHeight; if (winAlign.indexOf("right") != -1) leftPos = w-popWidth; 
		if (winAlign.indexOf("left") != -1) leftPos = 0; if (winAlign.indexOf("top") != -1) topPos = 0; 						
    features += (features.length > 0 ? ',' : '') + 'top=' + topPos+',left='+leftPos;}
  if (document.all && borderless && borderless != "" && features.indexOf("fullscreen") != -1) features+=",fullscreen=1";
  if (window["popupWindow"] == null) window["popupWindow"] = new Array();
  var wp = popupWindow.length;
  popupWindow[wp] = window.open(theURL,winName,features);
  if (popupWindow[wp].opener == null) popupWindow[wp].opener = self;  
  if (document.all || document.layers || document.getElementById) {
    if (borderless && borderless != "") {popupWindow[wp].resizeTo(popWidth,popHeight); popupWindow[wp].moveTo(leftPos, topPos);}
    if (alwaysOnTop && alwaysOnTop != "") {
    	ontopIntervalHandle = popupWindow[wp].setInterval("window.focus();", 50);
    	popupWindow[wp].document.body.onload = function() {window.setInterval("window.focus();", 50);}; }
    if (autoCloseTime && autoCloseTime > 0) {
    	popupWindow[wp].document.body.onbeforeunload = function() {
  			if (autoCloseTimeoutHandle) window.clearInterval(autoCloseTimeoutHandle);
    		window.onbeforeunload = null;	}  
   		autoCloseTimeoutHandle = window.setTimeout("popupWindow["+wp+"].close()", autoCloseTime * 1000); }
  	window.onbeforeunload = function() {for (var i=0;i<popupWindow.length;i++) popupWindow[i].close();}; }   
  document.MM_returnValue = (ignorelink && ignorelink != "") ? false : true;
}
//-->
</script>
</head>
<body>
<div id="contents">
<table width="740" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="120">&nbsp;</td>
  </tr>
  <tr>
    <td valign="top" class="content">
      <h1>checkout</h1>
      <form name="checkout_form" method="POST">
        <?php $chandlery_Index = $chandlery->DisplayIndex - 1;?>
        <?php
while (!$chandlery->EOF())      {
?>
          <?php $chandlery_Index ++;?>
          <input type="hidden" name="ID_<?php echo $chandlery_Index; ?>" value="<?php echo $chandlery->DisplayInfo("ID") ?>">
          <input type="hidden" name="Name_<?php echo $chandlery_Index; ?>" value="<?php echo $chandlery->DisplayInfo("Name") ?>">
          <input type="hidden" name="Description_<?php echo $chandlery_Index; ?>" value="<?php echo $chandlery->DisplayInfo("Description") ?>">
          <input type="hidden" name="Weight_<?php echo $chandlery_Index; ?>" value="<?php echo $chandlery->DisplayInfo("Weight") ?>">
          <input type="hidden" name="Quantity_<?php echo $chandlery_Index; ?>" value="<?php echo $chandlery->DisplayInfo("Quantity") ?>">
          <input type="hidden" name="Price_<?php echo $chandlery_Index; ?>" value="<?php echo $chandlery->DisplayInfo("Price") ?>">
          <input type="hidden" name="Colour_<?php echo $chandlery_Index; ?>" value="<?php echo $chandlery->DisplayInfo("Colour") ?>">
          <input type="hidden" name="Size_<?php echo $chandlery_Index; ?>" value="<?php echo $chandlery->DisplayInfo("Size") ?>">
          <?php
  $chandlery->MoveNext();
}
$chandlery->MoveFirst();
?>
        <table cellpadding="0" cellspacing="0" class="tableForm">
          <tr>
            <th> Name</th>
              <td><input name="Name" type="text" class="formTextField" value="<?php echo(ValidatedField("checkout","Name")) ?>"></td>
              <td><?php
if (ValidatedField("checkout","checkout"))  {
  if ((strpos((",".ValidatedField("checkout","checkout").","), "," . "1" . ",") !== false || "1" == ""))  {
    if (!(false))  {
?>
                <img src="images/error.gif" width="14" height="14"> Please enter your name
              <?php //WAFV_Conditional checkout.php checkout(1:)
    }
  }
}?></td>
            </tr>
          <tr>
            <th> Email</th>
              <td><input name="Email" type="text" class="formTextField" value="<?php echo(ValidatedField("checkout","Email")) ?>"></td>
              <td><?php
if (ValidatedField("checkout","checkout"))  {
  if ((strpos((",".ValidatedField("checkout","checkout").","), "," . "2" . ",") !== false || "2" == ""))  {
    if (!(false))  {
?>
                <img src="images/error.gif" width="14" height="14"> Please enter a valid email
              <?php //WAFV_Conditional checkout.php checkout(2:)
    }
  }
}?></td>
            </tr>
          <tr>
            <th> Credit Card No.</th>
              <td><input name="CreditCardNo" type="text" class="formCreditCardField" value="<?php echo(ValidatedField("checkout","CreditCardNo")) ?>"></td>
              <td><?php
if (ValidatedField("checkout","checkout"))  {
  if ((strpos((",".ValidatedField("checkout","checkout").","), "," . "3" . ",") !== false || "3" == ""))  {
    if (!(false))  {
?>
                <img src="images/error.gif" width="14" height="14"> Please enter your credit card number
              <?php //WAFV_Conditional checkout.php checkout(3:)
    }
  }
}?></td>
            </tr>
          <tr>
            <th> Card Type</th>
              <td><select name="CreditCardType" class="formDropDownField" id="CreditCardType">
                <option value=" " <?php if (!(strcmp(" ", (ValidatedField("checkout","CreditCardType"))))) {echo "selected=\"selected\"";} ?>> </option>
                <option value="VISA" <?php if (!(strcmp("VISA", (ValidatedField("checkout","CreditCardType"))))) {echo "selected=\"selected\"";} ?>>VISA</option>
                <option value="MasterCard" <?php if (!(strcmp("MasterCard", (ValidatedField("checkout","CreditCardType"))))) {echo "selected=\"selected\"";} ?>>MasterCard</option>
                <option value="AMEX" <?php if (!(strcmp("AMEX", (ValidatedField("checkout","CreditCardType"))))) {echo "selected=\"selected\"";} ?>>AMEX</option>
                <option value="Diners" <?php if (!(strcmp("Diners", (ValidatedField("checkout","CreditCardType"))))) {echo "selected=\"selected\"";} ?>>Diners</option>
              </select></td>
              <td><?php
if (ValidatedField("checkout","checkout"))  {
  if ((strpos((",".ValidatedField("checkout","checkout").","), "," . "4" . ",") !== false || "4" == ""))  {
    if (!(false))  {
?>
                <img src="images/error.gif" width="14" height="14"> Please select your card type
              <?php //WAFV_Conditional checkout.php checkout(4:)
    }
  }
}?></td>
            </tr>
          <tr>
            <td>&nbsp;</td>
              <td align="right"><input name="Checkout" type="submit" class="formCartButton" onMouseOver="this.className='formCartButtonOver';" onMouseOut="this.className='formCartButton';" value="Preview Purchase"></td>
              <td align="right">&nbsp;</td>
            </tr>
          </table>
      </form>      <h1>&nbsp;</h1></td>
  </tr>
</table>
</div>
</body>
</html>


#6 john_6767

john_6767
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 04 August 2006 - 02:47 AM

basically if that  else where i commented runs then i want it to load the next page and pass it the form values in my form on that next page..

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 August 2006 - 02:51 AM

Again, why are you using Javascript to set the action? Why can't you use a standard <form> tag?

Are you forgetting that PHP runs on the Server and is all finished by the time the Javascript executes on the Browser?

Ken

#8 john_6767

john_6767
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 04 August 2006 - 02:55 AM

if that standard form tag has an action set then the validation is bypassed.. i can get it to work with no validation. this is why i have put the javascript in the php else. the else checks to see if there are any errors and if there aren't then i want to post the form to the next page.. Is it possible to do what i am trying to do or is there a better way to do it?

#9 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 04 August 2006 - 02:55 AM

Your error is in the Javascript syntax.  You are forgetting semicolons at the ends of the lines, which is preventing any of the Javascript from being run.
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#10 john_6767

john_6767
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 04 August 2006 - 02:57 AM

aren't these semicolons? Do they have to be inside the php also?



#11 john_6767

john_6767
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 04 August 2006 - 03:00 AM

i think i know what u are saying.. i tried it like this anyway and it didn't work either..
print "<script language=\"JavaScript\">";
	print "document.checkout_form.action='checkout_confirm.php';";
	print "document.checkout_form.submit();";
	print "</script>";


#12 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 August 2006 - 03:20 AM

This doesn't make any sense:

if that standard form tag has an action set then the validation is bypassed..

Do mean if you put this form tag
<form name="checkout_form" method="POST" action="checkout_confirm.php">
the script checkout_confirm.php doesn't get executed?

Ken

#13 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 04 August 2006 - 03:24 AM

I think I get what you're saying, but it still doesn't make sense as to why you need this to be done by the Javascript.
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#14 john_6767

john_6767
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 04 August 2006 - 03:24 AM

no, if u were to put for the form
<form name="checkout_form" method="POST" action="checkout_confirm.php">
then it would load the next page fine, but my validation script would not be run,

i.e. you could pass blank or incorrect values to the next page..

This is why i have removed the action from the form and am trying to put it in so that it is run after the validation script is run..

#15 john_6767

john_6767
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 04 August 2006 - 03:25 AM

I think I get what you're saying, but it still doesn't make sense as to why you need this to be done by the Javascript.


how would i do this in php? I am only new to it.. would that be a better way to do it?

#16 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 04 August 2006 - 03:30 AM

Have your form action set to whatever.php.

Then do all of your error checking on whatever.php.  Use an if statement, and if errors are found, include form.php again and exit the script.  If errors aren't found, include process.php and exit the script.
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#17 john_6767

john_6767
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 04 August 2006 - 03:32 AM

mm.. sounds smart, but is there anyway to do it the way i was headed? Its just I had the pages going fine without validation..

#18 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 August 2006 - 03:32 AM

Where is your validation script? Is it Javascript or PHP.  I wouldn't rely on a purly Javascript validation routine, in fact, I don't use them as they can be bypassed very easily.

Ken

#19 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 04 August 2006 - 03:37 AM

99% of forms are processed in the way I outlined.  It's simple, easy to maintain, and secure.  I would recommend that over using Javascript wholeheartedly.  Some users may have it disabled... what then?  Will they just not be able to use your forms?  Javascript is also very easily taken advantage of by malicious users if you wish to validate your submissions that way.
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#20 john_6767

john_6767
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 04 August 2006 - 03:38 AM

my validation script is one of the include files in the top of that code i posted a few posts back, its php by webassist so its pretty secure.
Here's the code thats on the page, the stuff in the inlcudes just validates.. This is the bit i need to post the form to the next page if the validation passes...

<?php require_once("WA_ValidationToolkit/WAVT_Scripts_PHP.php"); ?>
<?php require_once("WA_ValidationToolkit/WAVT_ValidatedForm_PHP.php"); ?>
<?php
$chandlery->GetContent();
?>
<?php 
if (isset($_POST["Checkout"]))  {
  $WAFV_Redirect = "checkout.php";
  $_SESSION['WAVT_checkout'] = "";
  if ($WAFV_Redirect == "")  {
    $WAFV_Redirect = $_SERVER["SCRIPT_NAME"];
  }
  $WAFV_Errors = "";
  $WAFV_Errors .= WAValidateRQ(((isset($_POST["Name"]))?$_POST["Name"]:"") . "",true,1);
  $WAFV_Errors .= WAValidateEM(((isset($_POST["Email"]))?$_POST["Email"]:"") . "",true,2);
  $WAFV_Errors .= WAValidateCC(((isset($_POST["CreditCardNo"]))?$_POST["CreditCardNo"]:"") . "","4:51:52:53:54:55:34:37:30:36:38:",true,3);
  $WAFV_Errors .= WAValidateRQ(((isset($_POST["CreditCardType"]))?$_POST["CreditCardType"]:"") . "",true,4);

  if ($WAFV_Errors != "")  {
    PostResult($WAFV_Redirect,$WAFV_Errors,"checkout");
  }
  else {
  	print "<script language=\"JavaScript\">";
	print "document.checkout_form.action='checkout_confirm.php'";
	print "document.checkout_form.submit()";
	print "</script>";
  }
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users