PHP didn't see hard setup value for input when it is disabled

But... One problem is still appear. When I set up value for hard in HTML:

enter image description here

  <div class="flex">
    <div class="field">
      <input name="subject_field" id="subject_field" placeholder="Temat" value="Testing" disabled>
      <label class="icon" for="subject_field">
        <i class="fa fa-star fa-fw" aria-hidden="true"></i>
      </label>
    </div>
  </div>

My PHP code didn't see that in this field is something inside.

I want to have input for subject/topic and selection for subject/topic. I will be comment one of them if I don't need them. But also I want to have third variant - input for subject/topic with hard setup setup value. But this not working as I except.

Now that is the main problem. Why? And how to resolve this one. It is very important for me. Please advice.


UNDER IS THE MAIN QUESTION - one problem was solved by me.


Ok, I have to edit my question because I see that nobody can not understand me.

Well, so again: I have very low knowledge about PHP, but even so I created simply contact form only in PHP. It works very well but I would like to upgrade it and I don't know how.

That contact form looks that:

enter image description here

It is in Polish because I from Poland. I am asking here because we do not have in Poland place like this for asking help.

Now, what I would like to change? You can see field named "Temat" (well it is not name, but placeholder)? It means Topic. Now everybody can topic message as they want. I want to change it and set up "hard" topics. Then everybody will only can choice my topics. So what is the problem?

Even if I now setup hard "topic" via "value=something" (in HTML) then PHP thinks that this field is empty and of course return error. I have to setup variable in PHP code.

I suppose if I now create new field for list choice then PHP still will be think that field is empty. And I don't want to create variables for each topic. I would like to setup PHP for reading what is in field and setup variable by self.

Now here I don't know what to do and how. Because I am newbie. Don't tell me go back to book and learn whole PHP. Please advice.

Here is code of PHP: https://github.com/IdolwSzutrab7/Contact-Form/blob/master/submit.php It is little long and I don't know to paste it here or not?

<?php
function died($error) {
    echo '<p><h2>Wystąpił problem z działaniem skryptu.</h2></p>';
    echo '<p>Wykryto następujące błędy:<ul>'.$error.'</ul></p>';
    echo '<p>Wiadomośc nie została wysłana.</p>';
    echo '<p><a href="javascript:history.go(-1)">Kliknij tutaj</a>, aby wrócić i spróbować ponownie.</p>';
    //echo '<p><a href="javascript:window.close()">Kliknij tutaj</a>, aby zamknąć kartę i spróbuj ponownie..</p>';
    die();
};
function done() {
  echo '<p><h1>Wiadomość została wysłana.</h1></p>';
}
if ( isset($_POST) ) {
  // Adresat
  $do         = 'XXX'; // Adres e-Mail na który ma zostać wysłany formularz
  // Dane formularza
  $imie       = $_POST['name_field'];
  $email      = $_POST['address_field'];
  $telefon    = $_POST['phone_field'];
  $firma      = $_POST['company_field'];
  $temat      = $_POST['subject_field'];
  $wiadomosc  = $_POST['message_field'];
  $captcha    = $_POST['g-recaptcha-response'];
  // Nagłówek wiadomości e-Mail
  $headers = array('From: "'.$imie.'" <'.$email.'>',
                   'Reply-To: '.$email,
                   'X-Mailer: PHP/' . PHP_VERSION);
  $headers = implode("\r\n", $headers);
  // ReCaptcha NoCaptcha
  $secret = "XXX";
  if (!$captcha) {
    died('<li>Proszę wypełnić formularz reCAPTCHA.</li>');
    exit;
  }
  $response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
  // Wytyczne do sprawdzenia danych
  $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  $imie_exp = "/^[A-Za-z ęóąśłżźćńĘÓĄŚŁŻŹĆŃ]+$/";
  $telefon_exp = "/^[0-9 ]+$/";

  // Sprawdzanie formularza w poszukiwaniu błędów
  if ($response['success'] == false) { // Tutaj sprawdza, czy reCAPtCHa zwraca success
    died('<li>Walidacja reCAPTCHA nie przeszła poprawnie!<br />Twoje działanie zostało zarejestrowane, a dane przesłane do administratora.</li>');
    exit;
  } else if (  !isset($_POST['name_field'])    ||
               !isset($_POST['address_field']) ||
               !isset($_POST['message_field']) ) { // Tutaj jest bloczek, który sprawdza czy dane pola są uzupełnione
    died('<li>Nie wszystkie wymagane pola zostały wypełnione.</li>');       
  } else if ( !preg_match($email_exp,$email) ) { // Tutaj sprawdza, czy e-Mail jest poprawnie uzupełniony
    died('<li>Adres e-Mail nie wygląda na autentyczny.</li>');
  } else if ( !preg_match($imie_exp,$imie) ) { // Tutaj sprawdza, czy Imię lub Nick nie zawiera jakichś dziwnych znaków
    died('<li>Imię lub Nick zawiera nienaturalne znaki.</li>');
  } else if ( !preg_match($telefon_exp,$telefon) && !$telefon == null ) { // Tutaj sprawdza, czy telefon zawiera cyfry tylko wtedy, gdy jest coś wpisane [inaczej widzi pole ktore jest ukryte i trkatuje je jako blad]
    died('<li>Numer telefonu powinien zawierać wyłącznie cyfry.');
  } else if ( strlen($wiadomosc) < 10 || strlen($wiadomosc)>1000 ) { // Tutaj sprawdza długość wiadomości
    died('<li>Wiadomość jest zbyt krótka lub zbyt długa.');
  } else if ( strlen($imie)>50    ||
              strlen($email)>50   ||
              strlen($telefon)>50 ||
              strlen($firma)>50   ||
              strlen($temat)>100 ) { // Tutaj jest bloczek, który sprawdza długość poszczególnych pól
    died('<li>Przekroczono maksymalną ilość znaków w niektórych polach.</li>');
  } else { done(); mail($do, $temat, $wiadomosc, $headers); };
} else { 
  died('<li>Nastąpiło nieoczekiwane zatrzymanie skryptu!</li>');
};
?>

And here is code of form in HTML: https://github.com/IdolwSzutrab7/Contact-Form/blob/master/index.php It is also little long.

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" />
    <script type="text/javascript" src="//code.jquery.com/jquery-2.2.4.min.js"></script>
    <script type="text/javascript" src="//www.google.com/recaptcha/api.js"></script>
  </head>
<body>



<style>
body, html {margin:0;padding:0;}
#contact_form {
  width: 600px;;
  padding: 50px;
  margin: 25px auto;
  background: white;
  background: url('https://zotabox.com/media/core/image/gallery/simplepopup/1d.jpg');
  background-size: cover;
  box-shadow: 0px 0px 30px -5px rgba(0,0,0,0.5);
} #contact_form div.flex {
  display: flex;
  width: 100%;
} #contact_form div.field {
  position: relative;
  width: 100%;
  margin: 5px;
} #contact_form div.field input, #contact_form div.field textarea {
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  padding-left: 30px;
  outline: none;
  border: 1px solid rgba(0,0,0,0.2);
  border-radius: 4px;
  background: white;
  font-family: Arial;
  color: black;
} #contact_form div.field input:hover, #contact_form div.field input:focus,
#contact_form div.field textarea:hover, #contact_form div.field textarea:focus {
  border: 1px solid rgba(0,0,0,0.4);
} #contact_form div.field #submit_button {
  width: auto;
} #contact_form div.field #submit_button:hover {
  cursor: pointer;
} #contact_form div.field .icon {
  position: absolute;
  top: 9px;
  left: 7px;
  color: rgba(0,0,0,0.5);
  cursor: text;
} #contact_form div.field input:hover + .icon, #contact_form div.field input:focus + .icon,
#contact_form div.field textarea:hover + .icon, #contact_form div.field textarea:focus + .icon {
  color: rgba(0,0,0,0.7);
}
</style>

<form id="contact_form" method="post" action="submit.php">
  <div class="flex">
    <div class="field">
      <input name="name_field" id="name_field" placeholder="Twój nick lub imię">
      <label class="icon" for="name_field">
        <i class="fa fa-user fa-fw" aria-hidden="true"></i>
      </label>
    </div>
    <div class="field">
      <input name="address_field" id="address_field" placeholder="Twój adres e-Mail">
      <label class="icon" for="address_field">
        <i class="fa fa-envelope fa-fw" aria-hidden="true"></i>
      </label>
    </div>
  </div>
  <div class="flex">
    <div class="field" style="display: none;">
      <input name="phone_field" id="phone_field" placeholder="Twój numer telefonu">
      <label class="icon" for="phone_field">
        <i class="fa fa-phone fa-fw" aria-hidden="true"></i>
      </label>
    </div>
    <div class="field"  style="display: none;">
      <input name="company_field" id="company_field" placeholder="Firma">
      <label class="icon" for="company_field">
        <i class="fa fa-briefcase fa-fw" aria-hidden="true"></i>
      </label>
    </div>
  </div>
  <div class="flex">
    <div class="field">
      <input name="subject_field" id="subject_field" placeholder="Temat">
      <label class="icon" for="subject_field">
        <i class="fa fa-star fa-fw" aria-hidden="true"></i>
      </label>
    </div>
  </div>
  <div class="flex">
    <div class="field">
    <textarea name="message_field" id="message_field" placeholder="Tutaj wpisz treść swojej wiadomości..." rows="7" maxlength="1000" spellcheck="true"></textarea>
      <label class="icon" for="message_field">
        <i class="fa fa-pencil fa-fw" aria-hidden="true"></i>
      </label>
    </div>
  </div>
  <div class="flex">
    <div class="field">
      <div class="g-recaptcha" data-sitekey="6LfnGigTAAAAAGsECHWJRKpajpJI0SYt24XFy8S-"></div>
    </div>
    <div class="field" style="text-align: right;"><div style="display: inline-block; position: relative;">
    <input id="submit_button" type="submit" value="Wyślij">
      <label class="icon" for="submit_button" style="cursor: pointer;">
        <i class="fa fa-paper-plane fa-fw" aria-hidden="true"></i>
      </label>
    </div></div>
  </div>
</form>



</body>
</html>

Well... When I setup value for "hard" like this:

enter image description here

Then I have to setup variable for "hard" in PHP, like this:

enter image description here

Otherwise PHP return error while send form.

I am sure at 90% that same situation will be when I add selection field in the form - I mean PHP will not recognize choice from selection. That is the problem.

Hmm... I think I found what is wrong. My PHP code only check if the value of specified fields are set. Not if they contains something. Even if value is empty for PHP it means it is set. Please look at this section:

if (  !isset($_POST['name_field'])    ||
               !isset($_POST['address_field']) ||
               !isset($_POST['message_field']) )

So now the main question is how to rebuild it for checking if these values contains something not checking if they are set.

Upvotes: 0

Views: 201

Answers (4)

FullStack
FullStack

Reputation: 198

This code:

  <input name="subject_field" id="subject_field" placeholder="Temat" value="Testing" disabled>

should be:

  <input name="subject_field" id="subject_field" placeholder="Temat" value="Testing" readonly>

The disabled function in your code won't post the input value of "subject_field" to the PHP file.

Upvotes: 1

mior farhan
mior farhan

Reputation: 82

I am wondering why you have to make it like these $imie == NULL || $email == NULL || $temat == NULL || $wiadomosc == NULL

Upvotes: 0

Well... I think I resolve my problem (and appear another, please read to the end), but please check me and confirm.

I also give you test link but please don't spam too much - http://ts.isvn.pl/contactform/

The problem was from beginning with PHP code (I supposed that) but I didn't know where. I star thinking about that and I found:

enter image description here

  } else if (  !isset($_POST['name_field']) ||
               !isset($_POST['address_field']) ||
               !isset($_POST['subject_field']) ||
               !isset($_POST['message_field'])) { // Tutaj jest bloczek, który sprawdza czy dane pola są uzupełnione
    died('<li>Nie wszystkie wymagane pola zostały wypełnione.</li>');       
  }

I read a few interesting questions here at stackoverflow (i.e. "php is null or empty?") and understand that what I had was wrong. Again I started thinking about that and start changing. I found working solution:

enter image description here

  } else if (  $imie == NULL ||
               $email == NULL ||
               $temat == NULL ||
               $wiadomosc == NULL ) { // Tutaj jest bloczek, który sprawdza czy dane pola są uzupełnione
    died('<li>Nie wszystkie wymagane pola zostały wypełnione.</li>');       
  }

But that was not finish. I told you that I want my form modular. Some of fields I would like to turn on and turn off. I used just display: none in HTML:

enter image description here

  <div class="flex" style="display: none;">
    <div class="field">
      <input name="subject_field" id="subject_field" placeholder="Temat" value="Testing" disabled>
      <label class="icon" for="subject_field">
        <i class="fa fa-star fa-fw" aria-hidden="true"></i>
      </label>
    </div>
  </div>

But... For people like me this is hidden. But not for PHP code. I had to comment these lines. Otherwise PHP code still saw this code.

enter image description here

<!--
  <div class="flex" style="display: none;">
    <div class="field">
      <input name="subject_field" id="subject_field" placeholder="Temat" value="Testing" disabled>
      <label class="icon" for="subject_field">
        <i class="fa fa-star fa-fw" aria-hidden="true"></i>
      </label>
    </div>
  </div>
-->

So bad but I can handle this.

But... One problem is still appear. When I set up value for hard in HTML:

enter image description here

  <div class="flex">
    <div class="field">
      <input name="subject_field" id="subject_field" placeholder="Temat" value="Testing" disabled>
      <label class="icon" for="subject_field">
        <i class="fa fa-star fa-fw" aria-hidden="true"></i>
      </label>
    </div>
  </div>

My PHP code didn't see that in this field is something inside.

I want to have input for subject/topic and selection for subject/topic. I will be comment one of them if I don't need them. But also I want to have third variant - input for subject/topic with hard setup setup value. But this not working as I except.

Now that is the main problem. Why? And how to resolve this one. It is very important for me. Please advice.

I am post it as a answer because I resolve some of my problems and this message is too long for put it in the main question.

Upvotes: 0

mior farhan
mior farhan

Reputation: 82

If you want to make a selection so you just add a select tag in your html forms, just a simple as that. Also make sure that your value in selection correspond to your selection list.Below is the code

<select>
  <option value="topic1">Topic 1</option>
  <option value="topic2">Topic 2</option>
  <option value="topic3">Topic 3</option>
</select>

Upvotes: 1

Related Questions