2

Title is pretty explicit to what I'm trying to do. I want to retreive the variable vavlue in the URL and then send it to add.php.

The reason I don't simply put the PHP code in html is because of a FORM which also send var to add.php

PHP Code :

<?php

$id = $_POST["id"];

 ?>

Javascript in blabla.html?id=test

    <script>
 function getResults()
 {
  var xmlhttp;
  if (window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
  }
  else{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function(){
  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
  document.getElementById("here").innerHTML=xmlhttp.responseText;
  }
 }
 var id = getParameterByName("id");
 var data = "id="+document.getElementById("id").value;

 xmlhttp.open("POST","add.php",true);
 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 xmlhttp.send(data);
 }
</script>

<script>
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
</script>

2 Answers 2

0
var data = "id="+document.getElementById("id").value;
                                         ^^^^--string containing the letters 'i' and 'd'

should probably be

var data = "id="+document.getElementById(id).value;
                                         ^^-- variable named "id"
0
0

try this function

function getParameterByName( name ) {
  var parts = window.location.search.substr(1).split("&");
  var p_arr = {};
  for( var i = 0; i < parts.length; i++ ) {
    var temp = parts[i].split("=");
    if ( decodeURIComponent( temp[0] ) != "" ) {
      p_arr[decodeURIComponent( temp[0] )]  = decodeURIComponent( temp[1] );
    }
  }
  return p_arr[name];
}

function getResults() {
  var xmlhttp;
  if ( typeof XMLHttpRequest !== 'undefined' ) {
    xmlhttp = new XMLHttpRequest();
  }
  else {
    var versions  = [ "MSXML2.XmlHttp.5.0", "MSXML2.XmlHttp.4.0", "MSXML2.XmlHttp.3.0", "MSXML2.XmlHttp.2.0", "Microsoft.XmlHttp" ];
    for( var i = 0, len = versions.length; i < len; i++ ) {
      try {
        xmlhttp = new ActiveXObject( versions[i] );
        break;
      }
      catch(e) {}
    }
  }

  xmlhttp.onreadystatechange  = function() {
    if ( this.readyState === 4 && this.status == 200 ) {
      document.getElementById( "here" ).innerHTML = xmlhttp.responseText;
    }
  };

  var id    = getParameterByName( "id" );
  var data  = "id="+document.getElementById( id ).value;

  xmlhttp.open( "POST", "add.php", true );
  xmlhttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
  xmlhttp.send( data );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.