get url parameter using javascript and send it to php

55

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>
454

Answer

Solution:

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"
980

Answer

Solution:

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 );
}

People are also looking for solutions to the problem: php - images from ajax call onclick function

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.