简体   繁体   English

AJAX和表单交互上的提交按钮

[英]AJAX and submit button on form interaction

I'm creating a simple website and a html page on it which contains a table that shows products. 我正在创建一个简单的网站和一个html页面,其中包含一个显示产品的表格。 I load this table using AJAX and it work properly. 我使用AJAX加载了此表,它可以正常工作。 Here is a screenshot: 这是屏幕截图:

http://pokit.org/get/?1ff94a636771d2713849d74364cd4d1a.jpg

Under the table I have buttons which perform CRUD operations using AJAX. 在桌子下面,我有一些按钮可以使用AJAX执行CRUD操作。 They communicate to a php script on a server outside of my domain using GET method. 他们使用GET方法与我域外的服务器上的php脚本进行通信。 When I click on Add product it opens a form with a button that whose onclick event calls a function which adds a product using AJAX. 当我单击“添加产品”时,它会打开一个带有按钮的表单,该按钮的onclick事件调用一个函数,该函数使用AJAX添加产品。 But, when I click, the whole page reloads and the product is not added. 但是,当我单击时,整个页面将重新加载,并且未添加产品。 If I put the value that says wheter the call is async to false, it works as intended and the product is added to the table, however that is not the point of AJAX. 如果我输入的值表示调用是异步的,则为false,它可以按预期工作,并且将产品添加到表中,但这不是AJAX的重点。

This is my code for adding a product(delete and update are almost the same). 这是我添加产品的代码(删除和更新几乎相同)。

<div id="addProductPopup">
    <div id="popupContact">
        <form id="form" method="post" name="form">
            <img id="close" src="/servis/Resursi/Slike/close.png" onclick ="hide('addProductPopup');">
            <h2>Dodavanje proizvoda</h2>
            <hr>
            <input id="name" name="naziv" placeholder="Naziv proizvoda" type="text" required>
            <input id="kolicina" name="kolicina" placeholder="Količina proizvoda" type="text" required>
            <input id="url" name="url" placeholder="URL slike" type="text" required>
            <input type="submit" value="Pošalji" class="popupButtons" onclick="addProduct()">
        </form>
    </div>

When I click on submit this function is called: 当我单击提交时,此函数称为:

function addProduct(){

    var isValid = true;
    var url = "http://zamger.etf.unsa.ba/wt/proizvodi.php?brindexa=16390";

    var amount = document.form.kolicina.value;
    var naziv = document.form.naziv.value;
    var slikaurl = document.form.url.value;

    var validity = validateFields(naziv, slikaurl, amount);
    if(!validity) return false;
    var product = {
        naziv: naziv,
        kolicina: amount,
        slika: slikaurl
    };
    var requestObject = new XMLHttpRequest();
    requestObject.onreadystatechange = function(event) {
        if (requestObject.readyState == 4 && requestObject.status == 200)
        {
            loadProducts();
            event.preventDefault();
        }
    }
    requestObject.open("POST", url, true);
    requestObject.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    requestObject.send("akcija=dodavanje" + "&brindexa=16390&proizvod=" + JSON.stringify(product));
}

It is because you are not preventing the default action of the submit button click. 这是因为您没有阻止提交按钮单击的默认操作。

You can return false from an event handler to prevent the default action of an event so 您可以从事件处理程序返回false ,以防止事件的默认操作,因此

<input type="submit" value="Pošalji" class="popupButtons" onclick="addProduct(); return false;">

But since you have a form with a submit button, I think it will be better to use the submit event handler like 但是由于您的表单带有一个提交按钮,所以我认为最好使用如下的Submit事件处理程序

<form id="form" method="post" name="form" onsubmit="addProduct(); return false;">
    ....
    <input type="submit" value="Pošalji" class="popupButtons">

Your problem is that your submit button still executes a real submit. 您的问题是您的提交按钮仍然执行真实的提交。 You could change your addProducts method. 您可以更改addProducts方法。 The method have to return false to prevent the real submit. 该方法必须返回false以防止真正的提交。

Submit button performs default Submit action for HTML code. “提交”按钮对HTML代码执行默认的“提交”操作。

Try to change Submit tag into Button tag. 尝试将Submit标签更改为Button标签。 Or after AddProduct() in OnClick JS Action put 或在OnClick JS Action中的AddProduct()之后

return false;

简单更改将输入type =“ button”代替tpye =“ submit”

<input type="button" value="Pošalji" class="popupButtons" onclick="addProduct()">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM