0

I create a javascript + a php page. The script on the php page send data to my sql database, but don't show the result into on my home page.

Here the code

function getVote(question_ID) {
  var option_ID = document.querySelector("input[id='option']:checked").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 (option_ID.readyState == 4 && option_ID.status == 200) {
      document.getElementById(poll).innerHTML = option_ID.responseText;

    }
  }
  xmlhttp.open("GET", "./core/vote.php?user_ID=" + user_ID + "&question_ID=" + question_ID + "&option_ID=" + option_ID, true);
  xmlhttp.send();

}
<div id="poll">
</div>

More code

<div id="poll">
     <form method="post" onsubmit='getVote(question_ID);' >
        <p><?php echo $question ['question_content']; ?></p>

        <p>

 <?php
 $poll_option_selection = "SELECT option_ID, option_content FROM poll_options WHERE question_ID='$question_ID'" ;
 $poll_option_result = mysqli_query ($connect,$poll_option_selection) ;
 foreach ($poll_option_result as $poll_option) { 
 $option_ID = $poll_option ['option_ID'] ;
 $option_content = $poll_option ['option_content'] ;
 ?>
 <script type="text/javascript">
 var option_ID = <?php echo json_encode($option_ID) ; ?> ;
 </script>
 <input type="radio" id="option" value ='<?php echo $option_ID ?>'  required /> 
 <?php echo $option_content ?>
 </br>
 <?php
 }
 ?>
 <input type="submit">
 </form>
 </div>

Thank you for your help

Adou Pro
  • 51
  • 7

1 Answers1

2

It should be:

document.getElementById("poll").innerHTML=option_ID.responseText;

document.getElementById takes the argument (id of element) as a string.

cjs1978
  • 477
  • 3
  • 7
  • Thank you cjs ! Its work better but I have a second problem now.. The page redirect to a index.php/?. The "?" hide the results.. :/ – Adou Pro May 15 '17 at 08:29
  • You're welcome. I can't see any redirects in your code. Could you add some more code including the part that redirects to "index.php/?" ? – cjs1978 May 15 '17 at 08:41
  • I succed to supress the redirection with
    but now i have the same problem with the hided div.. :/
    – Adou Pro May 15 '17 at 08:50
  • I'm not sure, but if I understand your code correctly, you should somehow cancel out the default POST behaviour of the form. Maybe you should use event.PreventDefault on the form's submit event. https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault. POST and GET methods both "redirect" by default to the page that is set in the form's "action" attribute. – cjs1978 May 15 '17 at 09:17
  • Or try this:
    – cjs1978 May 15 '17 at 09:18
  • Just adding to my comment above: If no "action" is set in the
    , the page will reload itself when the form is posted (if nothing else is done either in PHP or with javascript). This is standard behavior in all browsers.
    – cjs1978 May 15 '17 at 09:42
  • Could you specify what's not working? Any errors in your developer tools / console? Also, maybe this can help: http://stackoverflow.com/questions/3350247/how-to-prevent-form-from-being-submitted – cjs1978 May 16 '17 at 20:23