-3

Dear users with your help and guidance I have achieved so far. Thanks to the community here. As some users felt that it was a duplicate question - sorry I changed the expected result. With members guidance i think i can achieve this.

I have a form with a textarea, a combo, a text box and other elements. First 1) I enter address in the textarea 2)I select a pincode - which is populated from a table 3)when pincode is selected the next text field is populated by the same table used in point (2) above. For this the page is refreshed with pincode and it display the place in the next text box. Every think ok. But what i typed in the textarea and what i selected in the combo is refreshed to blank. I need to replace what i typed and selected. The script used for collecting the pincode

<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.pin_code.options[form.pin_code.options.selectedIndex].value;
self.location='addschool.php?pin_code=' + val ;
}
</script> 

The php code below:

<form data-toggle="validator" role="form">
  <div class="form-group textareawidth has-feedback">
    <label for="address">Enter school address</label>
    <textarea class="form-control" pattern = "^[_A-z0-9]{1,}$" maxlength="150" rows ="3" name = "saddress" id="address" placeholder="Enter address with out pincode" required></textarea>
    <span class="glyphicon form-control-feedback" aria-hidden="true"></span>
    <span class="help-block with-errors"></span>
  </div>

  <div class="form-group textareawidth">
  <label for="pin">Pincode - School:</label>
  <?php
  echo "<select class='form-control' id='pin' onchange=\"reload(this.form)\" name ='pin_code' name=pin_code value='' >";
  echo "<option selected='selected'>Select Pincode </option>";

while($nt=mysqli_fetch_array($result)){//Array or records stored in $nt


echo "<option value=$nt[pin]>$nt[pin]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
</div>
<?php
$vplace="";
if(isset($_GET['pin_code']))
{
$temp=$_GET['pin_code'];
$quer="SELECT place FROM pincode where pin = $temp ";
$ex1=mysqli_query($dbcon,$quer) or die(mysql_error());
$count1=mysqli_num_rows($ex1);

if($count)
{
$row = mysqli_fetch_assoc($ex1);
$vplace = $row["place"];
}
else
{
echo '<script>';
echo 'alert("no such place found");';
echo '</script>';
}
}
?>

<div class="form-group textareawidth">
    <label for="place">Place</label>
<?php
 //echo "<input type ='text' class='form-control' name = 'splace' id='place' value =$vplace]>;";
  echo "<p class='form-control-static'>$vplace</p>";
 ?>
</div>

How can I achieve this ? Thanks

1 Answers1

-1

According to your last comment I update the answer.

Here is an example of what you want:

in your form page (here is form.php) put the following code.

form.php:

<!doctype html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(e) {
    $('#pin').change(function(e) {
        var ajaxData = {
            pinCode: $(this).val()
        };
        $.post('getPlace.php', ajaxData, function(response){
            $('#place').val(response);
        });
    });
});
</script>
</head>
<body>
<form id="myform" name="frmForm" method="post" action="doAction.php">
<label for="address">Enter school address:</label><br>
<textarea id="address" name="txtAddress"></textarea><br>
<label for="pin">Pincode - School:</label><br>
<select id="pin" name="cmbPin">
<?php
$db = mysqli_connect('localhost','root','','testdb') or die(mysqli_connect_error());
$query = 'SELECT pin FROM pincode';
$result = mysqli_query($db,$query);
while($row = mysqli_fetch_array($result)){
    echo "<option value=\"$row[pin]\">$row[pin]</option>" . PHP_EOL;;
}
?>
</select><br>
<label for="place">Place:</label><br>
<input id="place" name="txtPlace" type="text"><br>
<input id="submit" name="btnSubmit" type="submit" value="GO!">
</form>
</body>
</html>

And create an new .php file in the same directory called getPlace.php and put the php code that fetches place from your database.

getPlace.php:

<?php
if(isset($_POST['pinCode'])){
    $pinCode = $_POST['pinCode'];
    $db = mysqli_connect('localhost','root','','testdb') or die(mysqli_connect_error());
    $query = "SELECT * FROM pincode WHERE pin = '$pinCode'";
    $sql = mysqli_query($db, $query);
    $result = mysqli_fetch_array($sql);
    $place = $result['place'];
    echo $place;
}
?>

for more information about jQuery and Ajax functions and parameters read the Documentation.

  • Sir I want the text box to be filled with place field based on the selection in the combo. How to do it? If ajax is the solution how? – Venkat Suresh Nov 19 '15 at 16:54
  • which textbox?! you don't have any textbox in your form. but if you want to fill a textbox with the combo's selected item you don't need an ajax request. you just have to use jQuery and on combo's change event get the selected item's text and assign it to value of the textbox. – Arash DaneshAraste Nov 20 '15 at 01:23
  • Danesh Araste Thanks I did what you said but faced one problem which I asked above please guide me – Venkat Suresh Nov 20 '15 at 14:22
  • OK you completely change your question! so what's the problem with that code?! you don't ask anything. where is the problem you faced?! I suggest, if you want to have a live form and you want to fill the form field depending on your combo selection, use jQuery and Ajax. please tell where the problem exactly is, then I can help you. – Arash DaneshAraste Nov 20 '15 at 21:54
  • Thanks. The problem is I duno Ajax. I used a javascript to get the value selected in the combo (which itself is populated from a column in a table) and the page is refreshed and while doing so it pulls out a value from another column it displays in the corresponding text box. Example here is it gets the pincode selected and displays the place of the pincode in the textbox. The problem lies in the above code is while refreshing the first address textarea gets blank and similarly the combo box gets to default selected. I want both to retain the same values. – Venkat Suresh Nov 21 '15 at 10:09
  • When you reload the location using javascript, you lost the form data that the user previously filled. If you don't know Ajax and you don't want to use it, you should POST or GET the form variables by submitting the form on every reload and use them to fill form elements, which is not a good solution. I suggest you to read [jQuery Ajax](http://api.jquery.com/jquery.ajax/) and create a live form. – Arash DaneshAraste Nov 21 '15 at 11:46