0

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!

4
  • 1
    How are you expecting a $_POST to contain anything if you do not send any POSTed data with your request? Commented Feb 17, 2013 at 21:41
  • You are sending request to contact/sendContent.php?q="+contentId whereas you are accessing CId on other page? It should be $_POST['q'] Commented Feb 17, 2013 at 21:41
  • plus the previous comment, you are missing the id='contentArea' somewhere else Commented Feb 17, 2013 at 21:46
  • Oh my goodness, check these cases out how look like... Commented Feb 17, 2013 at 22:14

2 Answers 2

1

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"];
Sign up to request clarification or add additional context in comments.

1 Comment

When I pasted code in this forum I was rechecking several different thins (this is for incompatible* names) sorry for bad eng.
0

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;
?>

1 Comment

script is closed (bad copy paste). it works now :) Thx anyway.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.