1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Id form

Discussion in 'HTML & Website Design' started by alex4xss, Aug 2, 2017.

  1. #1
    hi guys,
    I have a problem with a contact form. The problems are two. Leaving the html code with identical ids (id = name name = id: id = name name = email), the error checking of the php file tells me that everything is fine, but in the mail I find all the same answers (obviously) . If I make them unique (id = name name = name, id = email email = email, etc) the php file makes me an error and my email does not give me the text. I hope I was clear. Does anyone have a solution or suggestion?
    The html code is this,
    <label>Nome e Cognome:</label>
                                <input type="text" class="form-control" id="name" name="nome" required data-validation-required-message="Please enter your name.">
                                <p class="help-block"></p>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-6">
                        <div class="control-group form-group">
                            <div class="controls">
                                <label>Email:</label>
                                <input type="text" class="form-control" id="name" name="email" required data-validation-required-message="Please enter your email address.">
                               </div>
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-6">
                        <div class="control-group form-group">
                            <div class="controls">
                                <label>Società</label>
                                <input type="text" class="form-control" id="name" name="societa" required data-validation-required-message="Please enter your society name.">
                                <p class="help-block"></p>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-6">
                        <div class="control-group form-group">
                            <div class="controls">
                                <label>Telefono</label>
                                <input type="text" class="form-control" id="name" name="telefono" required data-validation-required-message="Please enter your number.">
                                <p class="help-block"></p>
                            </div>
                        </div>
                    </div>
                    <div class="clearfix"></div>
                    <div class="col-lg-12 col-md-12 col-sm-12">
                        <div class="control-group form-group">
                            <div class="controls">
                                <label>Messaggio:</label>
                                <textarea rows="10" cols="100" class="form-control" id="messaggio" messaggio="messaggio" required data-validation-required-message="Please enter your message" maxlength="999" style="resize:none"></textarea>
                            </div>
    PHP:
    And this is the sending php code

    <?php
    ini_set('display_errors','On');
    error_reporting(E_ALL);
           $name = $_POST['nome'];
           $name = $_POST['email'];
           $name = $_POST['societa'];
            $name = $_POST['telefono'];
            $messaggio = $_POST['messaggio'];
           $from = 'From: My Contact Form';
           $to = 'info@visiondeluxe.it';
           $subject = 'Richiesta info dal sito';
    $totalmessage="
    Nome: $name \n
    Email: $name \n
    Società: $name \n
    Telefono: $name \n
    Messaggio: $messaggio \n";
    
           if (!isset($_POST['submit'])) {
               if (mail ($to, $subject, $from, $totalmessage)) {
               echo '<p>Il messaggio è stato inviato!</p>';
               } else {
               echo '<p>Ah! Riprova per cortesia</p>';
               }
          
          
        
              }
        ?>
    PHP:
    .
     
    Last edited by a moderator: Aug 5, 2017
    alex4xss, Aug 2, 2017 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Uhm, what now? That script shouldn't work at all. You're overwriting $name 4 times in the PHP-file, for once. And the correct way to do it is usually to set BOTH "id" and "name" to the same thing. The ID is mainly for hooking the label to it, which you don't do (the "label" should have a for="id_of_input_element", so it looks like <label for="name"> for instance) so that you're able to focus the input by clicking the label. You have quite a bit of garbage in there, and you haven't included the actual form-elements in the code you posted, but something like this should be enough:
    
    <label for="name">Nome e Cognome:</label>
    <input type="text" class="form-control" id="name" name="name" required placeholder="Please enter your name.">
    <label for="email">Email:</label>
    <input type="email" class="form-control" id="email" name="email" required placeholder="Please enter your email address.">
    <label>Società</label>
    <input type="text" class="form-control" id="society" name="society" required placeholder="Please enter your society name.">
    <label>Telefono</label>
    <input type="text" class="form-control" id="phone" name="phone" required placeholder="Please enter your number.">
    <label>Messaggio:</label>
    <textarea class="form-control" id="message" name="message" required placeholder="Please enter your message" maxlength="999" style="resize:none"></textarea>
    
    Code (markup):
    You'll notice I removed all the unnecessary bloat you have in there, which is probably some inept JS-method for validation or inline messages or something like that. It's crap, and you don't need it. Also, you should avoid using two different languages in the code. Either stick with your own language, or use English all the time. You can of course use your language in the label-text, but for IDs and such, it's usually better to just use English, as it will be easier for others to follow your code, if you need to output it for getting help.

    Now for the PHP:
    
    <?php
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    $from = 'From: My Contact Form';
    $to = 'info@visiondeluxe.it';
    $subject = 'Richiesta info dal sito';
    $totalmessage="Nome: $_POST['name'] \r\n
    Email: $_POST['email'] \r\n
    Società: $_POST['society'] \r\n
    Telefono: $_POST['phone'] \r\n
    Messaggio: $_POST['message'] \r\n";
    
    if (isset($_POST['submit'])) {
       if (mail($to, $subject, $from, $totalmessage)) {
         echo '<p>Il messaggio è stato inviato!</p>';
       } else {
         echo '<p>Ah! Riprova per cortesia</p>';
       }
    }
    ?>
    
    PHP:
    As you can see, I removed all the unnecessary variables - no need to make the POST-values into variables if you're only using them once. Also, your last check was for if (!isset($_POST['submit'])) - which means if SUBMIT is NOT set, then check and send mail? That didn't sound right to me...
    There is no error-checking here, nor is there any validation or filtering of the POST-content, which will mean this contact-form will end up with spamming millions of users pretty quickly, and get your IP blacklisted, but hey, that's a completely different problem.
     
    PoPSiCLe, Aug 2, 2017 IP
  3. alex4xss

    alex4xss Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Then, thank you for the answer and for the code suggestions. For html, I figured there was a mistake, so everything now is reported in the mail. As for the php, it gave me array conversion errors, so I left everything as it was, just changing the labels.
     
    alex4xss, Aug 4, 2017 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Array conversion errors? In the PHP I posted? There are no arrays there at all...? (well, $_POST is an array, but...) what was the exact errors you got?
     
    PoPSiCLe, Aug 5, 2017 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #5
    use var_dump() to output the variables and see if they hold what you expect them too
     
    sarahk, Aug 5, 2017 IP
  6. alex4xss

    alex4xss Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    error php:
    Notice: Array to string conversion in /home/visiondeluxe.it/public_html/php/index.php on line 14

    Notice: Array to string conversion in /home/visiondeluxe.it/public_html/php/index.php on line 15

    Notice: Array to string conversion in /home/visiondeluxe.it/public_html/php/index.php on line 16

    Notice: Array to string conversion in /home/visiondeluxe.it/public_html/php/index.php on line 17

    Notice: Array to string conversion in /home/visiondeluxe.it/public_html/php/index.php on line 17

    However, by entering the code as you wrote it, I get a page error. I had to enter a space between $ _POST and [.


    $totalmessage="Nome: $_POST ['name'] \r\n
    Email: $_POST ['email'] \r\n
    Società: $_POST ['society'] \r\n
    Telefono: $_POST ['phone'] \r\n
    Messaggio: $_POST ['message'] \r\n";
    
    PHP:
    So in the end I resolved with the correction of the labels in html

    An example on the code?

    Using the code as you wrote it gave me a page error, but entering a space between $ _POST and [,

    $totalmessage="Nome: $_POST ['name'] \r\n
    Email: $_POST ['email'] \r\n
    Società: $_POST ['society'] \r\n
    Telefono: $_POST ['phone'] \r\n
    Messaggio: $_POST ['message'] \r\n";
    
    PHP:

    the php is interrogated and it returns the error:


    Notice: Array to string conversion in /home/visiondeluxe.it/public_html/php/index.php on line 14

    Notice: Array to string conversion in /home/visiondeluxe.it/public_html/php/index.php on line 15

    Notice: Array to string conversion in /home/visiondeluxe.it/public_html/php/index.php on line 16

    Notice: Array to string conversion in /home/visiondeluxe.it/public_html/php/index.php on line 17

    Notice: Array to string conversion in /home/visiondeluxe.it/public_html/php/index.php on line 17

    Il messaggio è stato inviato!

    Then, i changed only the label on the html code and its ok
     
    Last edited by a moderator: Aug 5, 2017
    alex4xss, Aug 5, 2017 IP
  7. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #8
    arrays within strings are going to cause problems.
    if you don't know how to use a command go to php.net and look it up.

    here's how I would write the offending string that is causing problems

    
    $totalmessage="Nome: \t{$_POST ['name']}
    Email: \t{$_POST['email']}
    Società: \t{$_POST['society']}
    Telefono: \t{$_POST['phone']}
    Messaggio: \t{$_POST['message']} \r\n";
    
    PHP:
    but I'd feel really uncomfortable about sending it using unscreened $_POST variables with no checks for spam, or other problem content.
     
    sarahk, Aug 5, 2017 IP
  8. alex4xss

    alex4xss Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    Was a part of code suggested from PoPSiCLe. I resolved my problems, but ill try it and ill let u know
     
    alex4xss, Aug 6, 2017 IP
  9. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #9
    @alex4xss: I was typing quickly and wasn't clear. Arrays in strings can cause problems if they're not laid out properly. I put the {} which is a way of telling PHP exactly where the variable name starts and finishes and I thought that might explain the errors you were getting.
     
    sarahk, Aug 6, 2017 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #10
    double quoted strings can't use single quoted indexes. so when you "Something: $_POST['name']" that's invalid. you have to lose the single quotes and work with none at all.

    HENCE:

    $totalmessage="Nome: $_POST[name] \r\n
    Email: $_POST[email] \r\n
    Società: $_POST[society] \r\n
    Telefono: $_POST[phone] \r\n
    Messaggio: $_POST[message] \r\n";
    Code (markup):
    Being correct if using double quotes
    THAT SAID, double quoted strings PARTICULARLY with variable indexes are slower than string additions since it has to be parsed, parsed, and parsed again.

    As such, I'd write that as:
    $totalmessage =
    	'Nome: ' . $_POST['name'] . "\r\n" .
    	'Email: ' . $_POST['email'] . "\r\n" .
    	'Società: ' . $_POST['society'] . "\r\n" .
    	'Telefono: ' . $_POST['phone'] . "\r\n" .
    	'Messaggio: ' . $_POST['message'] . "\r\n";
    
    Code (markup):
    Though usually I have a define called CRLF that == "/r/n" so that only one memory reference is passed instead of making the parser work the same string over and over. That way it's guaranteed to be passed by reference instead of by direct address in the code.

    As a rule of thumb, I avoid double quoted strings unless I REALLY need the escape codes.

    ALL THAT SAID, lose the garbage placeholder rubbish, if they all get the same class assuming you have a fieldset none of them need classes, stop slopping presentation into the markup with style="", and for the love of christmas don't disable resize!!! You're just pissing on usability with that. If your layout can't handle someone resizing the textarea, your layout is rubbish.

    Also remember, ID has to be UNIQUE, you can only use it ONCE on a page, and it means JACK SHIT SERVER SIDE!!! The ID on your Input is for CLIENT side scripting, style hooks, or the target of the for="" attribute on your <label> tags. NAME="" is what gets sent to the server!

    You would also probably be having a lot less difficulty if you got rid of this halfwit mouth-breathing dumbass NONSENSE that bootcrap has duped you into thinking is how HTML should be written:

    
    <div class="col-lg-6 col-md-6 col-sm-6">
                        <div class="control-group form-group">
                            <div class="controls">
    
    Code (markup):
    Since that's REALLY putting the herp in the derp with div for nothing, presentational classes, classes for nothing, and all the other halfwit rubbish the snake oil that is bootcrap dupes you into using.
     
    Last edited: Aug 8, 2017
    deathshadow, Aug 8, 2017 IP
  11. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #11
    Ah, right - I never use the damn method I suggested, so I forgot the whole single-quote inside double-quotes - my bad :(
     
    PoPSiCLe, Aug 9, 2017 IP