简体   繁体   中英

javascript send form with get method

I try this dropdown form to send wrong data on page (movie information). This html code is dropdown form to send when user click on submit, javascript send GET data to another php file and there process data, but on place where is form data put Text: "Thank you"

Problem is: when i click on submit don't work, javascript is not doing its job.

html:

<div id='premijera' class='neispravniPodaci'>Report wrong data
    <div id='neispravnaForma' class='arrow_box' >
            <form name='frm1' action='' onSubmit='neispravniPod()' method='get'>
            <input class='textbox'  type='text' name='forma' placeholder='input for wrong data'>
            <input type='submit' name='sendForm' value='Send'  class='button'>
            </form>
        </div>
</div>

And javascript code is:

function neispravniPod()
{
var xmlhttp;
var forma=document.forms['frm1']['forma].value;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById('neispravnaForma').innerHTML='<t style=\"font-size:15px;font-weight:bold;color:black;\">Hvala!</t>';
    }
  }

xmlhttp.open('GET','glasanje.php?id=".$idfilm."&neispravno='+forma+'&ip=".$_SERVER["REMOTE_ADDR"]."&korisnik=".$userdata['user_id']."',true);
xmlhttp.send();
}

Like Rene points out, this is most likely a javascript syntax error, that is preventing the onSubmit function to kick in before the actual submit.

Suggest trying your code in jsfiddle.

I see PHP code in the xmlhttp.open request. How are you inserting that code? this might be a fail point if you are just trying to use that code in a js file and expecting the client to execute that (it will just spit out a syntax error, because a javascript file will never and should never interpret server side code).

Also , have you considered using jQuery or another js library that would help you in writing cleaner ajax requests?

Edit With Answer (according comment below):

Not sure what your PHP is for the rest of the file, but here is what I have used and tested locally to make sure it works. Corrected some syntax errors, also on the PHP side, the way you output variables. Paste this code in your index.php file or whatever you are using:

<div id='premijera' class='neispravniPodaci'>Report wrong data
    <div id='neispravnaForma' class='arrow_box'>
        <form name='frm1' action='javascript:neispravniPod();' method='get'>
            <input class='textbox' type='text' name='forma' placeholder='input for wrong data'>
            <input type='submit' name='sendForm' value='Send' class='button'>
        </form>
    </div>
</div>

<script type="text/javascript">
    function neispravniPod() {
        var xmlhttp;
        var forma = document.forms.frm1.forma.value;
        if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else { // code for IE6, IE5
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById('neispravnaForma').innerHTML = '<t style=\"font-size:15px;font-weight:bold;color:black;\">Hvala!</t>';
            }
        };

        xmlhttp.open('GET', 'glasanje.php?id=<?php echo $idfilm ?>&neispravno='+forma+'&ip=<?php echo $_SERVER["REMOTE_ADDR"] ?>&korisnik=<?php echo $userdata['user_id'] ?>', true);
        xmlhttp.send();
    }       
</script>

我要做的第一件事是检查javascript,好像您在脚本中有很多错误...我想在jsbin.com中检查我的脚本...只需打开javascript面板并将脚本粘贴到那里...我在您的脚本中发现了33条警告...单击: http : //jsbin.com/oqiliq/1/edit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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