猿问

如何从相同的表格中获得价值结果?

我如何从这个表格中获取价值结果?


<form name="forma" method="post" action="index.php"  id="dunja" >

    <p>

        Ime: <input id="ime" type="text" name="txtIme" value="" />

    </p>

    <p>

        Email: <input id="mail" type="text" name="txtEmail" value="" />

    </p>

    <p>

        <input type="submit" name="btnProsledi" value="Prosledi"  />

    </p>

</form>

而且,JS 是:


function sakupiPodatke(form){

    var delovi = [];

    var elementi = form.elements;


    for(var i=0; i<elementi.length; i++){

        var element = elementi[i];

        var naziv = encodeURIComponent(element.name);

        var vrednost = encodeURIComponent(element.value);


        delovi.push(naziv + "=" + vrednost);

    }


    return delovi.join("&");

}


var teloZahteva = sakupiPodatke(document.forma);


console.log(teloZahteva);

php文件也很简单:


<?php


$ime = $_POST["txtIme"];

$email = $_POST["txtEmail"];


?>

所以...我的问题是如何在 console.log 中从 JS 中读取变量“teloZahteva”?


子衿沉夜
浏览 100回答 3
3回答

一只斗牛犬

您的代码在页面首次加载时运行,在用户有机会填写表单之前。因此它将在控制台中显示空值。将代码放入在用户单击提交按钮时运行的事件侦听器中。document.getElementById("dunja").addEventListener("submit", function(e) {&nbsp; e.preventDefault();&nbsp; var teloZahteva = sakupiPodatke(e.target);&nbsp; console.log(teloZahteva);});function sakupiPodatke(form) {&nbsp; var delovi = [];&nbsp; var elementi = form.elements;&nbsp; for (var i = 0; i < elementi.length; i++) {&nbsp; &nbsp; var element = elementi[i];&nbsp; &nbsp; var naziv = encodeURIComponent(element.name);&nbsp; &nbsp; var vrednost = encodeURIComponent(element.value);&nbsp; &nbsp; delovi.push(naziv + "=" + vrednost);&nbsp; }&nbsp; return delovi.join("&");}<form name="forma" method="post" action="index.php" id="dunja">&nbsp; <p>&nbsp; &nbsp; Ime: <input id="ime" type="text" name="txtIme" value="" />&nbsp; </p>&nbsp; <p>&nbsp; &nbsp; Email: <input id="mail" type="text" name="txtEmail" value="" />&nbsp; </p>&nbsp; <p>&nbsp; &nbsp; <input type="submit" name="btnProsledi" value="Prosledi" />&nbsp; </p></form>这会在控制台中显示表单值,而不是将表单发送到 PHP。

慕码人2483693

在我的代码之后,我编写了这段代码,它说:未捕获的 ReferenceError:未定义 createXHRfunction formPost(url, form, callback){&nbsp; &nbsp; var xhr = createXHR();&nbsp; &nbsp; var data = sakupiPodatke(form);&nbsp; &nbsp; xhr.open("POST", url);&nbsp; &nbsp; xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");&nbsp; &nbsp; xhr.onreadystatechange = function(){&nbsp; &nbsp; &nbsp; &nbsp; if(xhr.readyState === 4){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var status = xhr.status;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((status >= 200 && status < 300) || status === 304){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback(xhr.responseText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Greskica!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; xhr.send(data);;}function procesOdgovora(odgovor){&nbsp; &nbsp; alert(odgovor);}formPost("index.php", document.forma, procesOdgovora);

慕尼黑的夜晚无繁华

<form name="forma" method="post" action="index.php"&nbsp; id="dunja" >&nbsp; &nbsp; &nbsp;<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ime: <input id="ime" type="text" name="txtIme" value="" />&nbsp; &nbsp; &nbsp;</p>&nbsp; &nbsp; &nbsp;<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Email: <input id="mail" type="text" name="txtEmail" value="" />&nbsp; &nbsp; &nbsp;</p>&nbsp; &nbsp; &nbsp;<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<input type="submit" name="btnProsledi" value="Prosledi" onclick="handleSubmit()"&nbsp; />&nbsp; &nbsp; &nbsp;</p>&nbsp;</form>handleSubmit(){&nbsp; var ime =getElementById('ime').value;&nbsp; var mail =getElementById('mail').value;}
随时随地看视频慕课网APP
我要回答