Jump to content

Recommended Posts

I know this isn't secure, i'm not using this for real.

 

I've created a form with 3 buttons and a text field. I would like it when a user clicks the amex button and when the number of numbers equal to 15 to confirm the number but if any of the other buttons are clicked and the numbers are equal to 16 then tell the card number.

 

For some reason i cant get it to work, I think maybe my values are wrong? idk. If someone could help that would be great thanks.

 

<?php
$amex = $_REQUEST["amex"];
$card = $_REQUEST["card"]);
$cardlen = strlen(trim($card));
$visa = $_REQUEST["visa"];
$master = $_REQUEST["master"];

if ($amex == 1 && $cardlen == 15) {
     echo "American Express Card Number:" . $card;
} elseif (($visa == 1 || $master == 1) && $cardlen == 16) {
     echo "Visa or Master Card Number:" . $card;
} else {
     echo "Please enter the correct card number";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/148089-php-if-statement-help/
Share on other sites

ok i will post both the form and then the full php code.

 

<html>

 

<body>

<p>Payment</p>

 

<form action="payment.php">

 

Cardholder's Name: <input type="text" name="name" />

<br>

Credit Card Number: <input type="text" name="card" />

<br>

Expiration Date: <input type="text" name="date" />

<br>

Security Code: <input type="text" name="code" />

<br>

<input type="radio" name="button" value="visa" /> Visa

<br>

<input type="radio" name="button" value="master" /> MasterCard

<br>

<input type="radio" name="button" value="amex" /> American Express

<br>

<input type="submit" value="Submit" />

</form>

 

 

</body>

 

</html>

 

-----------------------------------

<HTML>
<body>
<?php
if (strlen($_REQUEST["name"]) > 0) {
echo "Name:" . $_REQUEST["name"];
} else 
echo "You must insert your name";
?>
<br>
<?php
if (strlen($_REQUEST["date"]) > 0) {
echo "Expiration Date:" . $_REQUEST["date"];
} else 
echo "You must insert your expiration date";
?>
<br>
<?php
if (strlen($_REQUEST["code"]) > 0) {
echo "Security Code:" . $_REQUEST["code"];
} else 
echo "You must insert your security code";
?>
<br>
<?php
$amex = $_REQUEST["amex"];
$card = $_REQUEST["card"]);
$cardlen = strlen(trim($card));
$visa = $_REQUEST["visa"];
$master = $_REQUEST["master"];

if ($amex == 1 && $cardlen == 15) {
     echo "American Express Card Number:" . $card;
} elseif (($visa == 1 || $master == 1) && $cardlen == 16) {
     echo "Visa or Master Card Number:" . $card;
} else {
     echo "Please enter the correct card number";
}
?>

</body>

</HTML>

ok, next time, on your PHP page, do a print_r($_REQUEST) if it's not doing what you expect. you will see that there is no $_REQUEST['amex'] or $_REQUEST['visa'], etc. you use a radio button with the name 'button'. so there will be a $_REQUEST['button'] that is either equal to amex, visa, or master

ok i think im on the right track but still not working:

 

<?php
//$amex = $_REQUEST["amex"];
$card = $_REQUEST["card"]);
$cardlen = strlen(trim($card));
//$visa = $_REQUEST["visa"];
//$master = $_REQUEST["master"];

if ($card == amex && $cardlen == 15) {
     echo "American Express Card Number:" . $card;
} elseif (($card == visa || $card == master) && $cardlen == 16) {
     echo "Visa or Master Card Number:" . $card;
} else {
     echo "Please enter the correct card number";
}
?>

ok i fixed the quotes and fixed the variable for button. but i think im missing something else cause its not working.

 

<?php
$button = $_REQUEST["button"]);
$cardlen = strlen(trim($card));

if ($button == 'amex' && $cardlen == 15) {
     echo "American Express Card Number:" . $card;
} elseif (($button == 'visa' || $button == 'master') && $cardlen == 16) {
     echo "Visa or Master Card Number:" . $card;
} else {
     echo "Please enter the correct card number";
}
?>

the code worked for me. this is what i'm testing with...i altered the FORM tag so that i posts to the same page just for testing purposes:

<?php
  if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $card = $_REQUEST["card"];
    $button = $_REQUEST["button"];
    $cardlen = strlen(trim($card));
    if ($button == 'amex' && $cardlen == 15) {
      echo "American Express Card Number:" . $card;
    } elseif (($button == 'visa' || $button == 'master') && $cardlen == 16) {
      echo "Visa or Master Card Number:" . $card;
    } else {
      echo "Please enter the correct card number";
    }
  }
?>
<html>

<body>
<p>Payment</p>

<form method="post" action="">

Cardholder's Name: <input type="text" name="name" />
<br>
Credit Card Number: <input type="text" name="card" />
<br>
Expiration Date: <input type="text" name="date" />
<br>
Security Code: <input type="text" name="code" />
<br>
<input type="radio" name="button" value="visa" /> Visa
<br>
<input type="radio" name="button" value="master" /> MasterCard
<br>
<input type="radio" name="button" value="amex" /> American Express
<br>
<input type="submit" value="Submit" />
</form>


</body>

</html>

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.