douyi4991 2012-06-26 11:30
浏览 57
已采纳

使用Ajax从表单传递数据

Wondering if someone could help me. I have next to no knowledge with Ajax, and after many attempts at trying to pass my User1_id and Text through to my insert.php I've had no luck with inserting the data I want to my database.

As my profile.php and insert.php stands, the form submits to the database, but when the user submits the form it navigates to the insert.php and would rather have the Ajax send the data and stay on the Profile.php at all times, I feel this is a much better approach when creating such functions.

I'm wondering if someone can guide me in the right direction on how I'd go about doing this.

Profile.php Form

<form id="FormId" action="" method="get">
    <input type="hidden" value="<? echo $user1_id ?>">
        <textarea placeholder="Whats New??" id="FormId" name="status"></textarea>
            <input type="submit" name="Submit" value="Submit">
            </form>

Insert.php

    <?
    session_start();
    ini_set('display_errors',1);
    error_reporting(E_ALL|E_STRICT);
    include "db_connect.php";
    if (isset($_GET['status']))  {
         $status = $_GET['status'];
    }
    $user1_id=$_SESSION['id'];



if ($_GET['status']) {
$query = mysql_query("INSERT INTO feed (status,user1_id) VALUES ('$status', '$user1_id')")  or die (mysql_error());
if($query){
    echo "Posted Into Database";
}

exit();
}
?> 

I know I need something similiar to this.. But I think I'm stuck more on the var/data part.

$("form#myFormId").submit(function() {
    var mydata = $("form#myFormId").serialize();
    alert(mydata); // it's only for test
    $.ajax({
        type: "GET",
        url: "insert.php",
        data: mydata,
        success: function(response, textStatus, xhr) {
            alert("success");
        },
        error: function(xhr, textStatus, errorThrown) {
            alert("error");
        }
    });
    return false;
});

But don't know how to go about it.

Any guidence is appreciated. Not asking anyone to write the code out for me. But good direction is welcomed.

Thanks.

  • 写回答

2条回答 默认 最新

  • doulai2025 2012-06-26 23:25
    关注

    Just like to say thank you to both of you for your time and help. I re-wrote the code and have now got it working.

    Here is the final code PROFILE.PHP/JS

    <script type="text/javascript">
    
    function createstatus(content){
    $.post("insert.php", { user1_id: content } );
    refreshstream();
    }
    
    function createwallpost(content,user1_id){
    
    $.ajax({
       type: "POST",
       url: "insert.php",
       data: "status="+content+"&user1_id="+user1_id,
       success: function(){
         document.location = document.location;
       }
     });
    
    }
    </script>
    
        <form action="insert.php" method="POST" target="ifr1" class="form_statusinput">
            <input type="hidden" name="user1_id" value="<?php echo $user1_id ?>">
            <input type="text" name="status" id="status" class="homescreen_status_input" placeholder="Say something">
            <iframe name="ifr1" id="ifr1" style="display:none;"></iframe>
        </form>
    

    INSERT.PHP

    <?
    session_start();
    include "db_connect.php";
    if (isset($_POST['status']))  {
         $status = $_POST['status'];
    }
    
    $user1_id = $_POST['user1_id'];
    
    
    if ($_POST['status']) {
    $query = mysql_query("INSERT INTO feed (status,user1_id) VALUES ('$status', '$user1_id')")  or die (mysql_error());
    if($query){
        echo "Posted Into Database";
    }
    
    exit();
    } 
    
    
    ?> 
    

    I decided to go with post instead of the GET method, as I feel its securer and passes more information through smoothly. Thanks again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵