YesNoOk
avatar

[PHP] if/else (Read 9687 times)

Started by Jh0nnY2k, May 21, 2016, 05:13:31 pm
Share this topic:
[PHP] if/else
#1  May 21, 2016, 05:13:31 pm
  • ***
Quote
<h1>Contact</h1>
<form action="index.php?pag=contact" method="post">
<table>
    <tr>
        <td>Nume: </td>
        <td><input type="text" name="nume" /></td>
    </tr>
    <tr>
        <td>Email:</td>
        <td><input type ="text" name="email" /></td>
    </tr>
    <tr>
        <td>Subiect: </td>
        <td><input type="text" name="subiect" /></td>
    </tr>
    <tr>
        <td>Mesaj:</td>
    </tr>
    <tr>
        <td colspan="2">
            <textarea name="mesaj" cols="30" rows="10"></textarea>
        </td>
    </tr>
    <tr>
        <td>
            <input type="submit" name="trimite" value="Trimite" />
        </td>
    </tr>
</table>
</form>

<?php
if(isset($_POST['trimite'])){
print $_POST['nume']."</br>";
print $_POST['email']."</br>";
print $_POST['subiect']."</br>";
print $_POST['mesaj']."</br>";
}
else{

}
?>


So:))))))) how can i make the else (from the <?php ?> activate when i press "Trimite"??????  and not before
Last Edit: June 07, 2016, 04:25:51 pm by Jh0nnY2k
Re: [PHP] if/else
#2  May 21, 2016, 05:16:20 pm
  • ****
why do you expect the else to work if you want the condition on the "if" to be true?
I'm going to let god handle you people ✞
Re: [PHP] if/else
#3  May 21, 2016, 07:55:38 pm
  • avatar
  • ******
    • Thailand
The php code looks correct for checking if the trimite submit button was pressed. Not entirely sure what you're asking either.
Re: [PHP] if/else
#4  May 23, 2016, 06:11:48 pm
  • ****
  • Target Acquired.
    • Ukraine
    • mugencoder.com
That's not how your code will work. if-else blocks are designed to execute one block if the condition is true and execute the other if it is false. If you want your code to execute when the "Trimite" button is pressed, then you should just place it in the "if" block and get rid of the "else" block. Anything you place in your "else" block will execute when the form has not been submitted.

If that's not what you're talking about, then please be as specific as you can.

-[Все слова это только слова.]-
Re: [PHP] if/else
#5  June 04, 2016, 03:40:18 pm
  • ***
No, you're right. I was wrong. Thanks for the help.

Can't remember what exactly i wanted here.....it was something like when i press Trimite i wanted to have an error message if 'nume' 'mesaj' and so on don't have values. But now i know how to do that:) thank you very much.

Another question so  i do not open another topic  :


Quote
if(isset($_POST['trimite'])){
    $nume = $_POST['nume'];
    $subiect = $_POST['subiect'];
    $email = $_POST['email'];
    $mesaj = $_POST['mesaj'];
    $to = "gutza@demaimutza.ro";
   
    $body="";
    $body.="Nume: $nume \n";
    $body.="Email: $email \n";
    $body.="Subiect: $subiect \n";
    $body.="Mesaj : $mesaj";
    print $body;
   
}

Well, the "\n" doesn't work. I'm getting this result :
Quote
Nume: Ionut Email: Ionut Test Mesaj : Acest mesaj este un test
instead of getting a new row.


And a 2nd question :  can i run a script by pressing  a "Submit" input?(button)?
Quote
href="javascript: verificare()"

Edit: I'm guessing it doesn't work :(
Last Edit: June 04, 2016, 06:11:43 pm by Jh0nnY2k
Re: [PHP] if/else
#6  June 05, 2016, 07:05:18 pm
  • ****
  • Target Acquired.
    • Ukraine
    • mugencoder.com
If you're checking the browser for your message body, you won't see the breaks as "\n" isn't html. If you wanted a new line in the browser you'd have to use the
 tag. If you inspect the source you'll see that there's a new line in the actual source of your page though. This will be reflected in the email too when you eventually send it.

Yes, you can run a script before submitting the form. On the <form> tag add onsubmit="myJavascriptFunction()". Make sure that in your javascript function you are returning a boolean value as well (true or false). If you return false, the form will not submit, otherwise it will.

Something important to note here too. Your attempt didn't work because there is no "href" attribute on "input" elements. You'd have to use an <a> tag for that, but it wouldn't work as a button for submitting your form.

-[Все слова это только слова.]-
Re: [PHP] if/else
#7  June 07, 2016, 04:27:17 pm
  • ***
Thank you very much. Got it now. You're very helpful
Re: [PHP] if/else
#8  June 07, 2016, 05:01:08 pm
  • ****
  • Target Acquired.
    • Ukraine
    • mugencoder.com

-[Все слова это только слова.]-