0

I have the following code that checks if an email address is already registered if a user registers via form, and then sends a PIN reminder.

//check if email already taken
    $emailRes = mysqli_query($conn,"SELECT * FROM users WHERE user_email = '{$_POST['srEmail']}'");

    if(mysqli_num_rows($emailRes) != 0) {//if email found in table
        $emailRec = mysqli_fetch_assoc($emailRes);
        ?>
        Email already registered, would you like a PIN reminder?
        <form method="POST" action="">
        <input type="submit" name="srSend" value="Click here to get pin reminder" />
        <input type="hidden" name="srEmail" value="<?php echo $emailRec['user_email']?>" />
        </form>
        <?php
        exit;

    }

At present this then returns a blank page when the submit is complete; how would I add a "Success." message?

alias51
  • 8,178
  • 22
  • 94
  • 166
  • 4
    Warning: `mysqli_query($conn,"SELECT * FROM users WHERE user_email = '{$_POST['srEmail']}'");` is ripe for a SQL injection attack. – Ryan Jul 24 '13 at 16:48
  • your question isnt clear...when which submit is complete? the submit for get a pin reminder? or what? try and make the question easy to understand by posting more code from your page – oliverdejohnson Jul 24 '13 at 17:15

2 Answers2

1

The action attribute of a form specifies where to send the POST request with the input data. Right now it is blank which is not what you want. You need to send that data to another script which can then display whatever content you want.

Example:

Attribute:

action="after_form.php"

after_form.php

<?php

//do form processing

echo "Nice, you sent the form data here!";

You could also send people to the same PHP script, and then send different output for POST statements as indicated by Amal below.

thatidiotguy
  • 8,701
  • 13
  • 60
  • 105
  • 1
    That's not always the case. It's possible to sent a form to itself. – Amal Murali Jul 24 '13 at 16:49
  • Of course that is true, but what makes you think that is the case here. It is bad form in any case: http://stackoverflow.com/questions/1131781/is-it-a-good-practice-to-use-an-empty-url-for-a-html-forms-action-attribute-a – thatidiotguy Jul 24 '13 at 16:49
  • i agree with Amal and i think its the case here because the form is within the if statement and the processing logic references the same srEmail field – oliverdejohnson Jul 24 '13 at 16:56
  • @oliverdejohnson See my comment. Its generally agreed that having a blank action attribute is bad form. And your comment does not make any sense as the POST request includes the `srcEmail` value. – thatidiotguy Jul 24 '13 at 16:56
  • @oliverdejohnson what do you mean, I explained that the value of the action attribute specifies where the POST request is sent to. If he wants to do processing and display something, he will need to have logic at whatever script he specifies there to handle it. – thatidiotguy Jul 24 '13 at 17:01
  • the question is "how do i add a success message".you should answer that, if you can. with the way the code is and not try to force him change it to your way :) – oliverdejohnson Jul 24 '13 at 17:02
  • @oliverdejohnson whatever. I don't know why you assume he knows that is what is happening. You are assuming he wants it to go back to the same php script, but no code that is shown indicates this at all. – thatidiotguy Jul 24 '13 at 17:04
0

Just remove the closing php tag after the query and add this:

echo "Email already registered, would you like a PIN reminder?";

and then add the closing php tag.

This will display a message if the email already exists.

Len_D
  • 1,422
  • 1
  • 12
  • 21