ojc
ojc

Reputation: 23

PHP POST variables are empty

My problem is that I am trying to push one parameter via POST/GET, this parameter is passed to js function in coode bellow, all this files are in same directory.

Thy for help, answers. Best regards.

<div id="sidebar"> <?php include('showContent.js'); ?>
 <ul>
   <li>
   <h2>TITLE</h2>
      <ul>
    <li><a onclick="showContent('1');">Link1</a></li>
    <li><a onclick="showContent('2');">Link2</a></li>
    <li><a onclick="showContent('3');">Link3</a></li>
      </ul>
    </li>
  </ul>
</div>

showContent.js

<script>function showContent(cId)
 {
 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("contentArea").innerHTML=xmlhttp.responseText;
     }
   }
 xmlhttp.open("POST","contact/sendContent.php?cId="+cId,true);
 xmlhttp.send();
 }

sendContent.php

<?php
    $cId=$_POST["cId"];
    $tmp='error.php';

    switch ($cId) {
        case 1:{
            $tmp='contact.php';
            break;
        }
        case 2:{
            $tmp='idCard.php';
            break;
        }
        case 3:{
            $tmp='location.php';
            break;
        }

    }

    ob_start();
    include($tmp);
    echo ob_get_clean();

?>

P.S.: You should add hint on text editor's buttons how to use them, I spent a lot of time to figure out how to use this sucking editor for code formating.

Hint: select code pres this button!

Very easy when you know, very annoying if something do not behave like it should!

Upvotes: 0

Views: 164

Answers (2)

Kevin Garman
Kevin Garman

Reputation: 395

You've got several problems:
-as mentioned you need $_GET or $_REQUEST
-close your script tags in the JS file
-I'm not familiar with ob_start() but echo works great
-if the files are all in the same folder, then your AJAX request path is wrong

Here are working files...

<html>
<body>
<div id="sidebar"> <?php include('showContent.js'); ?>
 <ul>
   <li>
   <h2>TITLE</h2>
      <ul>
    <li><a onclick="showContent('1');">Link1</a></li>
    <li><a onclick="showContent('2');">Link2</a></li>
    <li><a onclick="showContent('3');">Link3</a></li>
      </ul>
    </li>
  </ul>
</div>
<div id="contentArea">content area</div>
</body>
</html>



<script type="text/javascript">function showContent(cId)
 {
 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("contentArea").innerHTML=xmlhttp.responseText;
     }
   }
 xmlhttp.open("POST","sendContent.php?cId="+cId,true);
 xmlhttp.send();
 }
</script>


<?php
    $cId=$_REQUEST["cId"];
    $tmp='error.php';

    switch ($cId) {
        case 1:
            $tmp='contact.php';
            break;

        case 2:
            $tmp='idCard.php';
            break;

        case 3:
            $tmp='location.php';
            break;
    }

    echo $tmp;
?>

Upvotes: 0

scartag
scartag

Reputation: 17680

Although you are using a POST request, you aren't really posting anything.

Since you have a query string appended you could access it.

$cId=$_REQUEST["q"];

Or

$cId=$_GET["q"];

Upvotes: 1

Related Questions