dongmian5325 2015-11-24 20:01
浏览 24
已采纳

提交没有表格的投入

I am trying to edit my table rows (img: http://imgur.com/yTpfCIc ) and POST the changed data to my edit.php file. I am trying to do this via jQuery. But when I click save button nothing happens, this is the explained javascript:

//Getting all "Edit" buttons in the table (one for each row)
var buttons = document.getElementsByClassName("clicker");

var savebutton = function(id){
    //Alert the Id of the button to see if the function is being called, and it is.
    alert(id);
    //If I have any $_POST["action"] different from "update" or "edit" I should get redirected to a page apologizing, saying this cant happen. But nothing happens. (not problem with edit.php, because already tried via another way of posting from another page)
    $.post( "edit.php", { action: "test"} );
};

var buttonclicked = function(e){
    if(e.target.textContent == "Edit")
    {         
        //In this function I create a lot of inputs that you see in the picture and cannot be submited one at a time
        e.target.textContent = "Cancel";
        var id = e.target.id;
        var editable_elements = document.querySelectorAll("[contenteditable=false]");
        var sub = document.getElementById("sub"+id);  

        var j = document.createElement("input");
        j.setAttribute("type", "text");
        j.setAttribute("name", "subject");
        j.setAttribute("value", sub.textContent);
        j.setAttribute("placeholder", sub.textContent);
        j.setAttribute("style", "width: 150px");
        j.textContent = sub.textContent;
        sub.innerHTML = "";
        sub.appendChild(j);

        for(var k = (id*6); k < (id*6)+6; k++){
            var l = k;
            var index = k -(k*id) + 1;

            l = document.createElement("input");
            l.setAttribute('type',"number");
            l.setAttribute("style", "width: 75px");
            l.setAttribute("step", "0.01");
            if(index <= 4){
                l.setAttribute('name',"g"+index);
                l.setAttribute('placeholder',"G"+index);
                l.setAttribute("value", editable_elements[k].textContent);
            }
            else if(index == 5){
                l.setAttribute('name',"creditos");
                l.setAttribute('placeholder',"credits");
                l.setAttribute("value", editable_elements[k].textContent);
            }
            else if(index == 6){
                l.setAttribute('name',"criteria");
                l.setAttribute('placeholder',"criteria");
                l.setAttribute("value", editable_elements[k].textContent);
            }
            editable_elements[k].innerHTML = "";
            editable_elements[k].appendChild(l);}

        //If any edit button is pressed, create a save button in the same row 
        var s = document.createElement("input");
        s.textContent = "Save";
        s.setAttribute('type',"button");
        s.setAttribute('value',"update");
        s.setAttribute("id", id);
        s.setAttribute("name", "a");

        //Call the function that is supposed to POST all inputs infotmations
        s.setAttribute("onclick", "savebutton(this.id)");
        var clickbutton = document.getElementById("save"+id);
        clickbutton.appendChild(s);

    }
    else //save button has been clicked
    {
        //nothing
    }
};

//If one of those buttons is clicked call the function
for(var j = 0; j < buttons.length; j++)
{
    buttons[j].addEventListener('click', buttonclicked);
}

That is my problem... If you want to see the whole page, its here: http://pastie.org/10578782

  • 写回答

1条回答 默认 最新

  • dongzhu7329 2015-11-24 20:06
    关注

    You have this comment in your code:

    //If I have any $_POST["action"] different from "update" or "edit" I should get redirected to a page apologizing, saying this cant happen. But nothing happens. (not problem with edit.php, because already tried via another way of posting from another page)

        $.post( "edit.php", { action: "test"} );
    

    When you post via ajax, redirects do not work. Open the dev tools in your browser and check the network traffic. I'm pretty sure the edit.php page is being posted to. You need to use a callback function to check the response of the post action and act accordingly, example:

    $.post( "edit.php", { action: "test"}, function(data) {
      //data is the response from the edit.php
      alert(data);
    });
    

    Try this code and see what the alert box says. If you want to "redirect", you can use document.location.href = 'whatever.php'; inside the callback in place of the alert(); statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)