1

I would like to get value from Form without submitting it, because the client have to choose the right type of house model to get the right form that fits the selected house model, without submitting, just selecting the house model and it for example continues the rest of form after that.

I have so far tried with this:

<form method="GET" action="foo.php">
  <select name="house_model">
    <option value="">------</option>
    <option value="<?php echo $model1;?>">Model 1</option>
    <option value="<?php echo $model2;?>">Model 2</option>
    <option value="<?php echo $model3;?>">Model 3</option>
  </select>
</form>

<?php    
$a = $_GET["housemodel"];

if($a<>'')
{
if($a == $model1)
{
   echo "<input type=\"text\" name=\"a\" value=\"something model1\">";
}
else if($a == $model2)
{
   echo "<input type=\"text\" name=\"b\" value=\"something model2\">";
} 
else if($a == $model3)
{
   echo "<input type=\"text\" name=\"c\" value=\"something model3\">";
}
}
?>        
3
  • 1
    You can not do it in this way, because page is already loaded with php script, you can do it by ajax. Commented Mar 28, 2013 at 8:12
  • user with java script and ajax Commented Mar 28, 2013 at 8:13
  • page is already loaded with php script. Commented Mar 28, 2013 at 8:18

5 Answers 5

1

I think, if you dont want page to be refreshed when user selects value from your drop down list, then you can use ajax. AJAX is used for this purpose. if you google, then you will find lots of tutorials related to AJAX.

Sign up to request clarification or add additional context in comments.

Comments

1

If you don't want to submit the form, use JavaScript to get the element. Or you can also use jquery to access the value. Make sure you put id = "something" and retrieve it using $('#something').val();

if you want to use JavaScript,

<select name="house_model" id="model">
    <option value="">------</option>
    <option value="<?php echo $model1;?>">Model 1</option>
    <option value="<?php echo $model2;?>">Model 2</option>
    <option value="<?php echo $model3;?>">Model 3</option>
</select>

<script type="text/javascript">
    var model= document.getElementById('model');
    alert("You entered: " + model);
</script>

AJAX codes here

 $("#model").live("change", function () {
     alert("You choose " + $('#model').val());
 });

Comments

1

IF you don't want to submit your form, you can get element using AJAX/jQuery.

<form method="GET" action="foo.php" onChange="getHouseModel">
  <select name="house_model" id="house_model">
    <option value="">------</option>
    <option value="<?php echo $model1;?>">Model 1</option>
    <option value="<?php echo $model2;?>">Model 2</option>
    <option value="<?php echo $model3;?>">Model 3</option>
  </select>
</form>

<script type='text/javascript'>
   function getHouseModel(){
      var model=$('#house_model').val();
      alert(model);
}
</script>

or you can write jquery like this. so you dont have to call function in tag

<script type="text/javascript">
    $('#house_model').select(function() {
       var model=$('#house_model').val();
      alert(model);
     });
</script>

2 Comments

How can i get this function into variable or something for comparing it with the $models?
can you get it with php and not use javascript/jquery code?
0

There is no way of getting data from a form before submitting in in PHP! This is because before submitting the form the PHP script simply is not called.

If you want to do some validation before sending data you must use javascript which is run on the client browser. Have a look at the jQuery library which makes this very easy: http://docs.jquery.com/Plugins/Validation/validate

If you want to use PHP only then you will need to separate the form into two pages.

Comments

0

You have to use ajax for achiving this.For example

<form method="GET" action="foo.php">
  <select name="house_model" id="house_model">
    <option value="">------</option>
    <option value="<?php echo $model1;?>">Model 1</option>
    <option value="<?php echo $model2;?>">Model 2</option>
    <option value="<?php echo $model3;?>">Model 3</option>
  </select>
</form>

 $("#house_model").live("change", function () {
   //Write the ajax and post the value to server side
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.