[PHP] if/else

Started by Jh0nnY2k, May 21, 2016, 05:13:31 PM

Previous topic - Next topic

Jh0nnY2k

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

Gennos

why do you expect the else to work if you want the condition on the "if" to be true?
Quote from: https://mugenguild.com/forum/msg.2469816I'm going to let god handle you people ✞

Rajaa Retired

The php code looks correct for checking if the trimite submit button was pressed. Not entirely sure what you're asking either.

Алексей

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.

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

Jh0nnY2k

#4
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  :


Quoteif(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 :
QuoteNume: 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)?
Quotehref="javascript: verificare()"

Edit: I'm guessing it doesn't work :(

Алексей

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.

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

Jh0nnY2k

Thank you very much. Got it now. You're very helpful

Алексей

Quote from: Jh0nnY2k on June 07, 2016, 04:27:17 PM
Thank you very much. Got it now. You're very helpful

No problem!

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