0

I'm trying to change an include path with JavaScript. It need's to go from this

<?php include 'sections/welcome.php'; ?>

to this

<?php include 'sections/new_visitor.php'; ?>

to this

<?php include 'sections/nda.php'; ?>

and so on... do anybody know how to code this in JavaScript?

2 Answers 2

1

you can't change how PHP is coded via javascript. You can send variables to php via javascript and then PHP responds to those variables... it looks like you have a registration of sorts. If you're using standard HTTP requests, you could use javascript to append a $_GET variable to the action link. For Instance:

 $('#form').attr('action', $('#form').attr('action') + '?page=welcome'); 

Then, upon clicking the link, PHP will have access to $_GET['page'], so in php you could:

 switch($_GET['page'])) {
     case 'welcome':
         include 'section/welcome.html';
         break;
     case 'nda':
         include 'section/nda.html';
         break;
 }
Sign up to request clarification or add additional context in comments.

5 Comments

This is how we do it for our CRM system so I second this method
ok! thanks :D but how do I connect this to feks. a submitbutton ?
oh wait, I think I see what you're asking... you would add the $_GET variable to the form action... see my updated answer.
look at my answer/question below :)
glad to hear it. Please post your solution so others can benefit, and if my answer helped, please accept it!
0

Look at the if(message ===1) statement below:) sorry for being such a noob :P

var xmlHttp = createXmlHttpRequestObject();

            function createXmlHttpRequestObject(){
                var xmlHttp;

                if(window.ActiveXObject){
                    try{
                       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
                    }catch(e){
                        xmlHttp = false;
                    }
                }else{
                    try{
                       xmlHttp = new XMLHttpRequest();
                    }catch(e){
                        xmlHttp = false;
                    } 
                }

                if(!xmlHttp){
                    alert("cat create that object hos");
                }



       function newuser(){
           if(xmlHttp.readyState ===0 || xmlHttp.readyState ===4){
             name = encodeURIComponent(document.getElementById('name').value);
             company = encodeURIComponent(document.getElementById('company').value);
             nationalities = encodeURIComponent(document.getElementById('nationality').value);
             phonenumber = encodeURIComponent(document.getElementById('phonenumber').value);
             queryString = "?name=" + name + "&?company=" + company + "&?nationalities=" + nationalities + "&?phonenumber=" + phonenumber + "&?URL= newuser";
             xmlHttp.open("GET", "/php/database.php?" + queryString, true);
             xmlHttp.onreadystatechange = handleServerResponse;
             xmlHttp.send(null);
           }else{
               setTimeout('newuser()', 1000);
           }
       }

       function handleServwerResponse(){
           if(xmlHttp.readyState ===4){
               if(xmlHttp.status === 200){
                   xmlResponse = xmlHttp.responseXML;
                   xmlDocumentElement = xmlResponse.documentElement;
                   message = xmlDocumentElement.firstChild.data;

                   if(message === 1){

//I want to call a function here, so i can change the include path

               }else{
                   document.getElementBy("underInput").innerHTML = 'User already exist klick to sign in here <p style="color:blue" onclick=""></p>';
               }
           }

       }
   }




}

1 Comment

I'm not sure exactly what you're trying to do. If you're simply trying to change what data the server is returning, you should just have the server return the appropriate data from the initial ajax call. Write your server return to return the data the javascript needs, rather than returning a status, and having javascript try to change what's being included. In your example, message === 1, can you write instead that message === 'whatever content javascript needs'?

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.