Jump to content

[SOLVED] PHP Help for school!


Joe Sai

Recommended Posts

Ok, so I'm doing this for school, and it is driving me insane! Basically, I'm supposed to create a script that validates whether a credit card number contains only integers. The script will remove dashes and spaces from the string. After the dashes and spaces are removed, the script should reject the credit card number if it contains any other nonnumeric characters.

 

So this is the script I came up with:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-…
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Validate Credit Card</title>
</head>

<body>
<H1> Validate Credit Card </H1><hr />
<?php
if (!isset($_GET['ccnumber']))
echo "<p>Enter your credit cad number.</p>";
else {
$Payment = $_GET['ccnumber'];
$ValidPayment = str_replace("-", "", $Payment);
$ValidPayment = str_replace(" ", "", $ValidPayment);
if (!is_numeric($ValidPayment))
echo "<p>You did not enter a valid credit card number!</p>";
else
echo "<p>Your credit cardnumber is $ValidPayment.</p>";
?>

<form action="ValidateCreditCard.php" method="get"
enctype="application/x-www-form-urlenc…
<p><input type="text" name="ccnumber" size="20" value=""<?php if
(!empty($_GET['ccnumber'])) echo $_GET['ccnumber'] ?> /></p>
<p><input type="submit" value="Validate Credit Card" />
</form><hr />

</body>
</html>

Only problem is everytime I open it in my web browser I get this:

 

24gmvc3.jpg

 

The code is all wacko. Can someone pleeeeease help me?

Link to comment
Share on other sites

That's what I'm trying to do, but it's not working. I uploaded it on 110mb.com and it won't display there, either. :(

 

I've been working on this for hours and am supposed to submit it tonight, but I don't even know if it is working properly because I can't even look at it. This is stressing me out :/

Link to comment
Share on other sites

You need to remember your curly brackets!

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-…
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Validate Credit Card</title>
</head>

<body>
<H1> Validate Credit Card </H1><hr />
<?php
if (!isset($_GET['ccnumber'])) {
echo "<p>Enter your credit cad number.</p>";
} else {
$Payment = $_GET['ccnumber'];
$ValidPayment = str_replace("-", "", $Payment);
$ValidPayment = str_replace(" ", "", $ValidPayment);
if (!is_numeric($ValidPayment)) {
echo "<p>You did not enter a valid credit card number!</p>";
} else {
echo "<p>Your credit cardnumber is $ValidPayment.</p>";
?>

<form action="ValidateCreditCard.php" method="get"
enctype="application/x-www-form-urlenc…
<p><input type="text" name="ccnumber" size="20" value=""<?php if
(!empty($_GET['ccnumber'])) echo $_GET['ccnumber'] ?> /></p>
<p><input type="submit" value="Validate Credit Card" />
</form><hr />

</body>
</html>

 

You can't go and mix it all up like you've done..

Link to comment
Share on other sites

That's what I'm trying to do, but it's not working. I uploaded it on 110mb.com and it won't display there, either. :(

 

I've been working on this for hours and am supposed to submit it tonight, but I don't even know if it is working properly because I can't even look at it. This is stressing me out :/

 

So, you decided to start developing php without a working test environment. Sorry, but that's ridiculous.

 

Giving me an error :/

 

You need to post your errors if you want help fixing problems.

Link to comment
Share on other sites

You need to remember your curly brackets!

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-…
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Validate Credit Card</title>
</head>

<body>
<H1> Validate Credit Card </H1><hr />
<?php
if (!isset($_GET['ccnumber'])) {
echo "<p>Enter your credit cad number.</p>";
} else {
$Payment = $_GET['ccnumber'];
$ValidPayment = str_replace("-", "", $Payment);
$ValidPayment = str_replace(" ", "", $ValidPayment);
if (!is_numeric($ValidPayment)) {
echo "<p>You did not enter a valid credit card number!</p>";
} else {
echo "<p>Your credit cardnumber is $ValidPayment.</p>";
?>

<form action="ValidateCreditCard.php" method="get"
enctype="application/x-www-form-urlenc…
<p><input type="text" name="ccnumber" size="20" value=""<?php if
(!empty($_GET['ccnumber'])) echo $_GET['ccnumber'] ?> /></p>
<p><input type="submit" value="Validate Credit Card" />
</form><hr />

</body>
</html>

 

You can't go and mix it all up like you've done..

 

Thank you sooooo much for your help.

 

The only problem is when I try to open that code, it says error on line 31, and line 31 is the </html>, so I have no idea why it would say that?

 

flekyb.jpg

 

@Thorpe. I do have wamp to run my php with, I just forgot to turn it on. Please don't be so rude with me, I don't know anything about PHP, this is my first and only PHP class I have ever taken. I'm trying my best and have worked at it all day, but it's really over my head, and I am kindly asking for help. Please try to understand that.

Link to comment
Share on other sites

I'm trying my best and have worked at it all day, but it's really over my head,

 

I'm sorry, but working on something all day without the ability to test it makes no sense either.

 

Now, the error message is exactly?

Link to comment
Share on other sites

I'm trying my best and have worked at it all day, but it's really over my head,

 

I'm sorry, but working on something all day without the ability to test it makes no sense either.

 

Now, the error message is exactly?

 

I have been trying to test it, I didn't even realize my php was turned off until someone pointed it out. Again, please don't treat me like I'm an idiot, this is the first PHP class I have ever taken, and I haven't the slightest clue about it. I'm trying the best I can, and ask that you please treat me with he same respect that I am treating you with.

 

I appreciate your help, but I ask that you please don't talk down to me, it's not very nice.

 

BTW, this is the error:

 

flekyb.jpg

 

Line 31 in the code is the </html> line

Link to comment
Share on other sites

You need to remember your curly brackets!

. . .

You can't go and mix it all up like you've done..

 

There was only one curly brace that was missing. You don't have to encapsulate else/if code if it is only on one line. Anyway, you need a closing curly brace after the line "Your credit card is valid".

 

There are two other errors:

 

1. The enctype value for the form is messed up. It needs to be corrected and, ost importantly, end with a double quote mark.

 

2. On the input field for the CC number you have code to repopulate the field, but you put the value AFTER the closing quote mark. The PHP needs to be inside the quotes, like so:

<p><input type="text" name="ccnumber" size="20" value="<?php if(!empty($_GET['ccnumber'])) echo $_GET['ccnumber'] ?>" /></p>

 

The only problem is when I try to open that code, it says error on line 31, and line 31 is the </html>, so I have no idea why it would say that?

 

because you had an opening bracket withut a closign bracket. So the PHP parser went to the end of the file and didn't find the closing bracket.

Link to comment
Share on other sites

You need to remember your curly brackets!

. . .

You can't go and mix it all up like you've done..

 

There was only one curly brace that was missing. You don't have to encapsulate else/if code if it is only on one line. Anyway, you need a closing curly brace after the line "Your credit card is valid".

 

There are two other errors:

 

1. The enctype value for the form is messed up. It needs to be corrected and, ost importantly, end with a double quote mark.

 

2. On the input field for the CC number you have code to repopulate the field, but you put the value AFTER the closing quote mark. The PHP needs to be inside the quotes, like so:

<p><input type="text" name="ccnumber" size="20" value="<?php if(!empty($_GET['ccnumber'])) echo $_GET['ccnumber'] ?>" /></p>

 

The only problem is when I try to open that code, it says error on line 31, and line 31 is the </html>, so I have no idea why it would say that?

 

because you had an opening bracket withut a closign bracket. So the PHP parser went to the end of the file and didn't find the closing bracket.

 

 

Of the problems you named, I don't understand what you mean by the first one, and I copied and pasted the second one into my code. The only problem is that I put the closing bracket in, but I'm still getting the same error.

 

This is what my code looks like now:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-…
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Validate Credit Card</title>
</head>

<body>
<H1> Validate Credit Card </H1><hr />
<?php
if (!isset($_GET['ccnumber'])) {
echo "<p>Enter your credit cad number.</p>";
} else {
$Payment = $_GET['ccnumber'];
$ValidPayment = str_replace("-", "", $Payment);
$ValidPayment = str_replace(" ", "", $ValidPayment);
if (!is_numeric($ValidPayment)) {
echo "<p>You did not enter a valid credit card number!</p>";
} else {
echo "<p>Your credit cardnumber is $ValidPayment.</p>";
}
?>

<form action="ValidateCreditCard.php" method="get"
enctype="application/x-www-form-urlenc…
<p><input type="text" name="ccnumber" size="20" value="<?php if(!empty($_GET['ccnumber'])) echo $_GET['ccnumber'] ?>" /></p>
<p><input type="submit" value="Validate Credit Card" />
</form><hr />

</body>
</html>

 

Sorry for being such a noob, I really don't know anything about php :/

Link to comment
Share on other sites

Ok, so I found that bracket I needed to close, thanks to you guys. :)

 

So at leas we're making ground! I'm getting it to show up, but now I have another problem:

 

8wihbk.jpg

 

 

This is my code:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-…
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Validate Credit Card</title>
</head>

<body>
<H1> Validate Credit Card </H1><hr />
<?php
if (!isset($_GET['ccnumber'])) {
echo "<p>Enter your credit cad number.</p>";
} else {
$Payment = $_GET['ccnumber'];
$ValidPayment = str_replace("-", "", $Payment);
$ValidPayment = str_replace(" ", "", $ValidPayment);
}
if (!is_numeric($ValidPayment)) {
echo "<p>You did not enter a valid credit card number!</p>";
} else {
echo "<p>Your credit cardnumber is $ValidPayment.</p>";
}
?>

<form action="ValidateCreditCard.php" method="get"
enctype="application/x-www-form-urlenc…
<p><input type="text" name="ccnumber" size="20" value="<?php if(!empty($_GET['ccnumber'])) echo $_GET['ccnumber'] ?>" /></p>
<p><input type="submit" value="Validate Credit Card" />
</form><hr />

</body>
</html>

Link to comment
Share on other sites

Ok, tried altering the code some more and got this now:

 

2rpvcqp.jpg

 

This is my code:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-…
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Validate Credit Card</title>
</head>

<body>
<H1> Validate Credit Card </H1><hr />
<?php
if (!isset($_GET['ccnumber'])) {
echo "<p>Enter your credit cad number.</p>";
} else {
$Payment = $_GET['ccnumber'];
$ValidPayment = str_replace("-", "", $Payment);
$ValidPayment = str_replace(" ", "", $ValidPayment);
}
if (!is_numeric($ValidPayment))
echo "<p>You did not enter a valid credit card number!</p>";
else 
echo "<p>Your credit cardnumber is $ValidPayment.</p>";

?>

<form action="ValidateCreditCard.php" method="get"
enctype="application/x-www-form-urlencoded">
<p><input type="text" name="ccnumber" size="20" value="<?php if(!empty($_GET['ccnumber'])) echo $_GET['ccnumber'] ?>" /></p>
<p><input type="submit" value="Validate Credit Card" />
</form><hr />

</body>
</html>

 

I don't understand how to fix the line 18 problem :/

Link to comment
Share on other sites

You are missing the curl brace ({}) after the if and else statements.

 

This:

if (!is_numeric($ValidPayment))
echo "<p>You did not enter a valid credit card number!</p>";
else 
   echo "<p>Your credit cardnumber is $ValidPayment.</p>";

 

Should be:

if (!is_numeric($ValidPayment)){
echo "<p>You did not enter a valid credit card number!</p>";
}else{
   echo "<p>Your credit cardnumber is $ValidPayment.</p>";
}

 

Thats what i think the problem is anyway.

Link to comment
Share on other sites

You are missing the curl brace ({}) after the if and else statements.

 

Braces aren't required with simple if's. (I would however get in the habbit of using them always)

 

The problem now is your using the $ValidPayment variable outside of the check for its existence.

 

<?php
if (!isset($_GET['ccnumber'])) {
  echo "<p>Enter your credit cad number.</p>";
} else {
  $Payment = $_GET['ccnumber'];
  $ValidPayment = str_replace("-", "", $Payment);
  $ValidPayment = str_replace(" ", "", $ValidPayment);
  if (!is_numeric($ValidPayment)) {
    echo "<p>You did not enter a valid credit card number!</p>";
  } else {
    echo "<p>Your credit cardnumber is $ValidPayment.</p>";
  }
}
?>

Link to comment
Share on other sites

OMG!!!!!!!!!!!!!!!!!!!

 

Thank you, thank you, THANK YOU! You're my hero! :D Works like a dream, I can finally submit it after 6 hours of nonstop work (embarrassing, I know :P).

 

I can't tell you how much I appreciate this, thanks so much!

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.