Jump to content

Recommended Posts

echo "<html><script type='javascript'>function check(){
card_no = document.form.card_no.value;
if(card_no.value.length < 16){
alert('card value blank');
return false;
}
if(name==""){
alert('enter your name');
return false;
}
if(exp_date==""){
alert('card value blank');
return false;
}
}</script><body>";
echo "<form name=form method='POST' action='?'>";
echo "Name  ";
echo "<input type='text' name='name' size='20'><br>";
echo "Card No.  ";
echo "<input type='text' name='card_no' size='20'><br>";
echo "Expiry Date  ";
echo "<input type='text' name='exp_date' size='20'><br>";
echo "CVV  ";
echo "<input type='text' name='cvv' size='20'><br>";
echo "Amount  ";
echo "<input type='text' name='amt' size='20'><br>";
echo "<input type='submit' name='confirm' value='Confirm' onclick='return check();'>";
echo "<input type='reset' name='reset'>";
echo "</form>";
echo "</body></html>";
Link to comment
https://forums.phpfreaks.com/topic/35942-solved-error-else-part/
Share on other sites

You haven't posted exactly what the problem is, but the first statement in your code won't work as you are not escaping the double quotes within the string - try putting \ before the internal " as below 

[code]
echo "<html><script type='javascript'>function check(){
card_no = document.form.card_no.value;
if(card_no.value.length < 16){
alert('card value blank');
return false;
}
if(name==\"\"){
alert('enter your name');
return false;
}
if(exp_date\"\"){
alert('card value blank');
return false;
}
}</script><body>";
[/code]
echo "<html><script type='text/javascript'>function check(){

len = document.form.card_no.value.length
card=document.form.card_no.value
n=0
if(len != 16 && len != 15){
alert('Enter valid card value');
return false;
}

var cardNo = document.form.card_no.value
  var cardexp = /^[0-9]{13,19}$/;
  if (!cardexp.exec(cardNo))  {
    alert('enter number only');
    return false;
  }


if(document.form.name.value.length < 1){
alert('enter ur name');
return false;
}
if(document.form.exp_date.value.length < 4){
alert('enter expiry date');
return false;
}
}</script><body>";
echo "<form name=form method='POST' action='?'>";
echo "Name  ";
echo "<input type='text' name='name' size='20'><br>";
echo "Card No.  ";
echo "<input type='text' name='card_no' size='20'><br>";
echo "Expiry Date  ";
echo "<input type='text' name='exp_date' size='20'><br>";
echo "CVV  ";
echo "<input type='text' name='cvv' size='20'><br>";
echo "Amount  ";
echo "<input type='text' name='amt' size='20'><br>";
echo "<input type='submit' name='confirm' value='Confirm' onclick='return check();'>";
echo "<input type='reset' name='reset'>";
echo "</form>";
echo "</body></html>";

if(isset($_POST['confirm'])){
$name = $_POST['name'];
$card_no = $_POST['card_no'];
$exp = $_POST['exp_date'];
$cvv = $_POST['cvv'];
$amount = $_POST['amt'];
---code---
}

javascript does not run! it has no effect on the form. it is suppose to validate the form fields.
If you are echo'ing large amounts of HTML (including Javascript and CSS) use [url=http://uk2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc]herdoc syntax[/url]

Also rather than alerti'ing one error message at a time, show all the errors on big alert box. I have done this for you.
[code]<?php

echo <<<HTML
<html>
<head>
<script type="text/javascript">
function check() {

    var error = new Array();
    var error_msg = "";

    var frm = document.getElementById("form");

    if(frm.name.value == "") {
        error[0] = "Eenter your Name";
    }

    if(frm.card_no.value.length < 16) {
        error[1] = "Card No. left blank or is invalid";
    }

    if(frm.exp_date.value == ""){
        error[2] = "Card expirary date left blank";
    }

    if(error.length > 0) {

        for(i = 0; i <= (error.length)-1; i++) {
            if(error[i] && error[i] != "") {
                error_msg += "\\t- " + error[i] + "\\n";
            }
        }

        alert("Please fix the following errors:\\n\\n" + error_msg);

        return false;
    } else {
        return true;
    }
}
</script>
</head>
<body>

<form id="form" method="POST" action="?passed=true" onSubmit="return check();">
  Name: &nbsp; <input type="text" name="name" size="20"><br />
  Card No.: &nbsp; <input type="text" name="card_no" size="16"><br />
  Expiry Date: &nbsp; <input type="text" name="exp_date" size="20"><br />
  CVV: &nbsp; <input type="text" name="cvv" size="20"><br />
  Amount: &nbsp; <input type="text" name="amt" size="20"><br />
  <input type="submit" name="confirm" value="Confirm"> &nbsp;
  <input type="reset" name="reset">
</form>

</body>
</html>
HTML;

?>[/code]
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.