-3

Html Code

<div class="container">
    <?php echo $_POST['name']; ?>
    <form method="POST" id="reg-form" action="register">
        <label>Full Name:</label>
        <input class="register-form-input" type="text" name="name" id="name"><br>
            <label>Username:</label>
        <input  class="register-form-input" type="text" name="username" id="username"><br>
            <label>Email:</label>
        <input  class="register-form-input" type="email" name="email" id="email"><br>
        <label>ID Number:</label>
        <input  class="register-form-input" type="text" name="idnumber" id="idnumber"><br>
            <label>Password:</label>
        <input  class="register-form-input" type="password" name="password" id="password"><br>
            <label for="name">Repeat Password:</label>
        <input  class="register-form-input" type="password" name="password1" id="password1"><br><br>
        <input type="submit"  name="submit"  id="register-submit" onclick="ajax_post();">
    </form>
    <?php echo $_POST['name']; ?>
</div>

Javascript File

function ajax_post(){
    // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "register/index";
    var name = document.getElementById("name").value;
    var username = document.getElementById("username").value;
    var vars = "name="+name+"&username="+username;
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
      if(hr.readyState == 4 && hr.status == 200) {
        var return_data = hr.responseText;
        console.log(return_data);
      }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
}
document.querySelector('#reg-form').onsubmit=function(){
        return false;
}

Register Controller

<?php 
class Register extends Controller{

    public function __construct(){
        parent::__construct();
        echo $_POST['name'];
    }
    public function index(){
        echo $_POST['name'];
        $this->view->render('register/index',false);
        $this->model->run();
        echo $_POST['name'];
    }
    public function run(){

    }
}

Issue is that When i return Data (ajax Response) in console it shows whole content and everywhere i have written echo $_POST['name'] appears only in console response,on page it's undefined. if you need to explain i'll make it more detailed. just $_POST['name'] is undefined on page,but not in console.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
DummyTarget
  • 112
  • 1
  • 2
  • 10
  • 1
    simple, it's because of your `` that is throwing that error at you. – Funk Forty Niner Feb 25 '18 at 17:50
  • in your first code, `$_POST['name']` will never have a value since it comes from an ajax request ; surely it won't be defined in the calling script/page ; upon receiving the ajax request, you have to update the page with javascript – Pierre Feb 25 '18 at 17:52
  • uhmm, could you be more specific? – DummyTarget Feb 25 '18 at 17:54
  • i know that is because of $_POST['name'] but i can't figure out why Ajax doesn't send data to Register controller, url="register/index" and in index method is written $_POST['name'] but it's undefined as well,it's like doesn't get any data,but in reality it gets – DummyTarget Feb 25 '18 at 17:56
  • http://pixs.ru/showimage/testpng_8254408_29486553.png here is an img, i have underlined Main part (asdasd) there is $_POST['name'] – DummyTarget Feb 25 '18 at 18:00
  • this post should not have been reopened, the story is clear here. I also rolled the question back; it is a duplicate. – Funk Forty Niner Feb 25 '18 at 18:33
  • @FunkFortyNiner — It is not a duplicate of [this](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef). The question says that the value does not show up as undefined in the console log. It is asking why the page hasn't updated after the Ajax request. – Quentin Feb 25 '18 at 18:52

1 Answers1

-1

Ajax is not time travel.

This is what happens:

  1. You make a request for a page to display in the browser window
  2. The value is undefined and shows as such in the page
  3. You use JavaScript to make a second, similar request to be handled with JS
  4. The value is defined and shows correctly when you log the response

Making a second request does not change history. The results of the first request are unchanged and are still shown in the page.

If you want to load a whole new page, then don't use Ajax.

If you want to modify the page the user is already looking at based on the responce you get to your Ajax request, then you must write JavaScript (in your onreadystatechange function) to manipulate the DOM of the page.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • man i just want to send data to Register_Model.php simple MVC structure, but data transfers to php after refresh only. – DummyTarget Feb 25 '18 at 18:36
  • @DummyTarget — It transfers to PHP when you send the Ajax response. That just isn't reflected in the page because that page was sent before you made the Ajax response … and you've done nothing to update it. – Quentin Feb 25 '18 at 18:45
  • and how to update? i mean somehow i got it because if i do document.querySelector('body').innerHTML=respons_data it updates and PHP gets value,but how to pass it without updating page? + it gets some bugs – DummyTarget Feb 25 '18 at 18:48
  • @DummyTarget — If you want to change what the user sees then you have to update the page! You should probably design your API so it is less of a sledge hammer and just returns pure data (e.g. as JSON) and then update the specific bits you care about. – Quentin Feb 25 '18 at 18:51
  • i have to do same if i don't care for UI and just want to send this javascript data to Php register ? – DummyTarget Feb 25 '18 at 18:54
  • As mentioned repeatedly: The PHP is getting the value you send. You said yourself that you could see it being correctly echoed in console.log. – Quentin Feb 25 '18 at 18:56
  • Yes it gets,but for some reasons doesn't go to database,goes only if refreshes. without refresh data is passed to php but can't go to database – DummyTarget Feb 25 '18 at 18:57
  • Why would it go into the database? The only thing you do with anything in `$_POST` is echo it. – Quentin Feb 25 '18 at 18:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165787/discussion-between-dummytarget-and-quentin). – DummyTarget Feb 25 '18 at 19:01