Keep the value of form submit with PHP


Posted on 2015-06-30 07:20:29


First we go to build a form. Here I will use a form to enter information into the database more customers I created the following form  
<Form action = "" method = "post">
  <Label> Customer Name </ label>
  <Input type = 'text' name = 'txtName' size = '30' value = '' /> <br/>
  <Label> Email </ label>
  <Input type = 'text' name = 'txtemail' size = '30' value = '' /> <br/>
  <Label> Address </ label>
  <Input type = 'text' name = 'txtaddress' size = '30' value = '' /> <br/>
  <Label> Phone </ label>
  <Input type = 'text' name = 'txtPhone' size = '30' value = '' /> <br/>
  <Label> Gender </ label>
  Men <input type = "radio" name = 'gender' size = '30' value = '0' checked = "checked" />
  Female <input type = "radio" name = 'gender' size = '30' value = "1" /> <br/>
  <Label> Citizenship </ label>
  <Select name = 'Country'>
      <Option value = ""> Choose </ option>
      <Option value = "1"> the Vietnam </ option>
      <Option value = "2"> Laos </ option>
      <Option value = "3"> Cambodia </ option>
  </ Select>
  <br />
  <Input type = "submit" value = "Get info" name = "ok" />
</ Form>
At the processor php code we handle as follows
if (isset ($ _ POST ['ok'])) {
       $ Name = $ email = $ address = $ phone = $ gender = $ country = "";  
       if ($ _ POST ['txtName'] == NULL) {
          echo "Please enter customer name";
       } Else {
          $ Name = $ _ POST ['txtName']; 
       }
       if ($ _ POST ['txtemail'] == NULL) {
          echo "Please enter the email address";
       } Else {
          $ Name = $ _ POST ['txtemail']; 
       }
       if ($ _ POST ['txtaddress'] == NULL) {
          echo "Please enter address";
       } Else {
          $ Name = $ _ POST ['txtaddress']; 
       }
       if ($ _ POST ['txtPhone'] == NULL) {
          echo "Please enter the phone number";
       } Else {
          $ Name = $ _ POST ['txtPhone']; 
       }
       if ($ _ POST ['country'] == NULL) {
          echo "Please select the country";
       } Else {
          $ Name = $ _ POST ['country']; 
       }
       $ Gender = $ _POST ['gender'];
       if ($ name && $ address gender && $ && && $ phone $ country) {
          echo $ name. "<br/>";
          echo $ addresses. "<br/>";
          echo $ phone. "<br/>";
          echo $ gender. "<br/>";
          echo $ country. "<br/>";
       }
    }

Handling  retain value for form submit PHP

<Form action = "" method = "post">
  <Label> Customer Name </ label>
  <Input type = 'text' name = 'txtName' size = '30' value = "<? Php if (isset ($ _ POST ['txtName']) && $ _ POST ['txtName']! = NULL) {echo $ _ POST ['txtName']; }?> "/> <br/>
  <Label> Email </ label>
  <Input type = 'text' name = 'txtemail' size = '30' value = "<? Php if (isset ($ _ POST ['txtemail']) && $ _ POST ['txtemail']! = NULL) {echo $ _ POST ['txtemail']; }?> "/> <br/>
  <Label> Address </ label>
  <Input type = 'text' name = 'txtaddress' size = '30' value = "<? Php if (isset ($ _ POST ['txtaddress']) && $ _ POST ['txtaddress']! = NULL) {echo $ _ POST ['txtaddress']; }?> "/> <br/>
  <Label> Phone </ label>
  <Input type = 'text' name = 'txtPhone' size = '30' value = "<? Php if (isset ($ _ POST ['txtPhone']) && $ _ POST ['txtPhone']! = NULL) {echo $ _ POST ['txtPhone']; }?> "/> <br/>
  <Label> Gender </ label>
  Men <input type = "radio" name = 'gender' size = '30' value = '0' <? Php if (isset ($ _ POST ['gender']) && $ _ POST ['gender'] == 0) {?> checked = "checked" <? php}?> />
  Female <input type = "radio" name = 'gender' size = '30' value = '1' <? Php if (isset ($ _ POST ['gender']) && $ _ POST ['gender'] == 1) {?> checked = "checked" <? php}?> /> <br/> 
  <Label> Citizenship </ label>
  <Select name = 'Country'>
      <Option value = "" selected = "selected"> Choose </ option>
      <Option value = "1" <? Php if (isset ($ _ POST ['country']) && $ _ POST ['country'] == 1) {?> Selected = "selected" <? Php}?>> Vietnam Men </ option>
      <Option value = '2' <? Php if (isset ($ _ POST ['country']) && $ _ POST ['country'] == 2) {?> Selected = "selected" <? Php}?>> Laos </ option>
      <Option value = '3' <? Php if (isset ($ _ POST ['country']) && $ _ POST ['country'] == 3) {?> Selected = "selected" <? Php}?>> Cambodia </ option>
  </ Select>
  <br />
  <Input type = "submit" value = "Get info" name = "ok" />
</ Form>
So we finished. You do it again and run on your local ... 
Good luck

tutscode.com