Jump to content

submitting form problem..


john_6767

Recommended Posts

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.

[code]  else {
print "<script language=\"JavaScript\">";
print "document.checkout_form.action='checkout_confirm.php'";
print "document.checkout_form.submit()";
print "</script>";
}
[/code]
Link to comment
Share on other sites

ok heres the code

[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>
[/code]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

i think i know what u are saying.. i tried it like this anyway and it didn't work either..
[code]print "<script language=\"JavaScript\">";
print "document.checkout_form.action='checkout_confirm.php';";
print "document.checkout_form.submit();";
print "</script>";[/code]
Link to comment
Share on other sites

This doesn't make any sense:
[quote]if that standard form tag has an action set then the validation is bypassed..[/quote]
Do mean if you put this form tag
[code]<form name="checkout_form" method="POST" action="checkout_confirm.php">[/code]
the script checkout_confirm.php doesn't get executed?

Ken
Link to comment
Share on other sites

no, if u were to put for the form
[code]<form name="checkout_form" method="POST" action="checkout_confirm.php">[/code]
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..
Link to comment
Share on other sites

[quote author=nethnet link=topic=102932.msg409315#msg409315 date=1154661854]
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.
[/quote]

how would i do this in php? I am only new to it.. would that be a better way to do it?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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...

[code]<?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>";
 }
}
?>[/code]
Link to comment
Share on other sites

Okay, well, I can tell you why your JavaScript isn't working.  Part of the DOM is that you cannot dynamically alter the attributes of something that hasn't yet been created.  For instance, you are trying to set an action to your form and submit it before you even create that form in the HTML.  You will have to put that segment of PHP/JavaScript after you create your form or else it will not work.
Link to comment
Share on other sites

ok ihave tried to do that but unsuccessful so far,

i have put this code in the else so that the function is called


[code]  print "<script language=\"JavaScript\">";
print "loadNewPageFunc();";
print "</script>";[/code]


then i have put after the form

[code] <script language="JavaScript">
function loadNewPageFunc()
document.checkout_form.action='checkout_confirm.php';
document.checkout_form.submit();
</script>[/code]
Link to comment
Share on other sites

ok, it looks like this now, but doesn't work yet..
[code] <script language="JavaScript">
function loadNewPageFunc(){
document.checkout_form.action='checkout_confirm.php';
document.checkout_form.submit();}
</script>[/code]

do i need the semicolons still? I have seen functions that don't have them at the end of every line.
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.