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 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析