doushishi6513 2014-08-31 12:45
浏览 63

使用ajax提交表单后如何在div中显示元素? (不刷新页面)

How to display element in div after submit form using ajax ? (Not refresh page)

.....................................................................................................................................................................

Normal i use this way

First select radio button and then press submit button, it's will display data (with refresh page)

I want to submit form using ajax and display element in div and not refresh page

how can i do that ?

HTML

<form name="f1" method="POST" action="">
<input type="radio" name="color" value="red">red
<br>
<input type="radio" name="color" value="black">black
<br>
<input type="submit" name="submit" value="OK">
</form>

PHP

<?PHP
if ( $_GET["color"] != "" )
    {
        $sql = "SELECT * FROM products WHERE color = '$_GET[color]' order by id asc";
        $result = mysql_query($sql);
        $datas=mysql_fetch_array($result);{
        $product_name = stripslashes(str_replace('
', '<br>',($datas['product_name'])));
        }
        echo $product_name;
    }
?>
  • 写回答

1条回答 默认 最新

  • douzheng5717 2014-08-31 12:54
    关注

    Try this

    HTML

    <form name="f1" method="POST" action="">
    <input type="radio" name="color" value="red">red
    <br>
    <input type="radio" name="color" value="black">black
    <br>
    <input type="submit" name="submit" value="OK">
    </form>
    
    <!-- This element will show ajax request -->
    <div id="result"></div>
    

    Javascript

    $(document).ready(function(){
      $("form[name=f1]").submit(function(){
        $.ajax({
           url : 'yourphpscript.php',
           type : 'GET',
           data : $(this).serialize(),
           success : function(data){
              $("#result").html(data);
           }
        });
        //!This is important to stay the page without reload
        return false;
      });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题