How to get VALUE from FORM without Submitting it?

后端 未结 5 1143
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 23:06

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

相关标签:
5条回答
  • 2020-12-01 23:33

    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());
     });
    
    0 讨论(0)
  • 2020-12-01 23:37

    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
        });
    
    0 讨论(0)
  • 2020-12-01 23:39

    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.

    0 讨论(0)
  • 2020-12-01 23:39

    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>
    
    0 讨论(0)
  • 2020-12-01 23:42

    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.

    0 讨论(0)
提交回复
热议问题