SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 47 of 47
  1. #26
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,358
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Hi,

    Thanks for making a test page - very helpful.

    You need to learn how to use the console.
    Here's a brief tutorial on how to do that.

    It says:
    Code:
    POST http://coverman.se/myScript.php 404 (Not Found)
    Which means that your AJAX call is not finding your PHP script.

    Sorry for the short reply, but the web app I am currently working on, has just exploded into a million pieces
    Last edited by Pullo; Aug 24, 2013 at 00:33.

  2. #27
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Does anyone know how to make this work? Just to save the text that is written into three different strings.

  3. #28
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,358
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Hi,

    So did you fix the issue with your AJAX script not being able to find the PHP script?

  4. #29
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Oh, sorry I didn't see your previous post. My page wasn't updated. I will look at it now.

  5. #30
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Very strange. Maybe it was case sensitive? Now I made it lower case. so the link is called myscript.php and IT IS on the server.
    If you test this link, it's there. Right? http://www.coverman.se/myscript.php

  6. #31
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,358
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Ok, progress.
    If you look at the console now however, you'll see:

    Code:
    XMLHttpRequest cannot load http://www.coverman.se/myscript.php. Origin http://coverman.se is not allowed by Access-Control-Allow-Origin.
    Could you ammend this:

    Code:
    url: "http://www.coverman.se/myscript.php"
    to this:

    Code:
    url: "myscript.php"

  7. #32
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Changed that now.

  8. #33
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,358
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Good. Now the AJAX script is talking to the PHP script.

    Unfortunately, it is not returning us any values.

    Can you post the contents of myscript.php?

  9. #34
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This is my script.php

    PHP Code:
    <?php
      $header 
    $_POST['header'];
      
    $content $_POST['content'];
      
    $footer $_POST['footer'];

      echo 
    "You submitted: " $header ", " $content ", " $footer;
    ?>

  10. #35
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,358
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Ok, that's weird.
    Give me a while and I'll set everything up on my local machine, so we are both able to run the same code.

  11. #36
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,358
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Ok, found the problem - simple really.

    Change:

    Code:
    var headerText = $("#header").text(), 
        contentText = $("#content").text(), 
        footerText = $("#footer").text();
    To:

    Code:
    var headerText = $("#header").val(), 
        contentText = $("#content").val(), 
        footerText = $("#footer").val();
    And things should work as expected.

  12. #37
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No, it's not working. I changed it to this:
    PHP Code:
    <button id="save">Save!</button>
       <
    script>
    $(
    "#save").on("click", function(){  
    var 
    headerText = $("#header").val(), 
        
    contentText = $("#content").val(), 
        
    footerText = $("#footer").val(); 

      $.
    ajax({
        
    type"POST",
        
    url"myscript.php",
        
    data'header=' headerText '&content=' contentText '&footer=' footerText ,
        
    success: function(res) {
          
    console.log(res);
       }
      });
    });
       
    </script> 

  13. #38
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,358
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    No, it is working!

    You are now submitting the three values to your PHP script and it successfully echos them back to you.

    Please look at the tutorial I linked to previously, so that you know how to view the output of console.log(res).

    Obviously, having your PHP script simply spit the values back at you, is not very useful, so you should modify it to have the values inserted into the db instead.

  14. #39
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    But is the code on my test page working for you?
    http://coverman.se/test.html

    It's not working for me. The my script.php is just echoing empty.
    It says: You submitted: , ,

    And the save button isn't taking me there. I have to write the myscript.php url manually after clicking save. So I guess it's not working.
    Can you try my test page again and see if that one is working for you.

  15. #40
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,358
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Quote Originally Posted by brad62 View Post
    But is the code on my test page working for you?
    Yes, it's doing exactly what it should.

    Quote Originally Posted by brad62 View Post
    It's not working for me. The my script.php is just echoing empty.
    It says: You submitted: , ,

    And the save button isn't taking me there. I have to write the myscript.php url manually after clicking save.
    I fear you're missing the point of AJAX.
    It's asynchronous, so you never leave the original page.
    The save button should not take you there.

    Of course, if you access the PHP script directly you will see "You submitted: , , "
    The output is being logged to the console.
    You should really read the tutorial I linked to previously.

  16. #41
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,358
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Here's a screenshot:

    brad.jpg

  17. #42
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ok. Well, I did read the tutorial, but probably not understood it all. So, the myscript.php is only working in the background. That's where I'm supposed to write the insert into db code? So, when a user is clicking on the save button it will do the INSERT from myscript.php and then they have to continue to a new page where my website is moving on to?

    Isn't there a way to make the SAVE button take the user to a new page? So, when they have entered the text they click on the button and it takes them to test_two.php Or do I have to have the SAVE button to store the things and then another button that say CONTINUE? Is that the best way to use this?

  18. #43
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,358
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Quote Originally Posted by brad62 View Post
    So, the myscript.php is only working in the background. That's where I'm supposed to write the insert into db code?
    Correct.

    I would probably do it like so:

    User enters input into header, content and footer areas.
    User clicks "Save and proceed"
    This fires off an ajax request (as we have set up here) to a PHP script.
    The PHP script inserts the data to the database, then returns a code indicating that the insert was successful.
    The success callback picks up this code and then redirects the user to the next page.

    e.g.

    Code:
    success: function(code) {
         if (code === "success"){
             window.location.replace("http://mysite.com/page2.html");
        }
    }
    Job done

  19. #44
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Sounds right, but where is that success code generated and how? If I have the mysql insert on the my script.php
    How would that code be generated? Do you mean that I should put a string after the INSERT into db like this:
    PHP Code:
    $code 'success'
    Or am I missing something here?

  20. #45
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,358
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Exactly.
    Maybe wrap the insert in a try - catch block, then return the code after a successful insert.

    Does that make sense?

  21. #46
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    645
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Sorry, didn't understand that last one.
    "Maybe wrap the insert in a try - catch block, then return the code after a successful insert."

  22. #47
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,358
    Mentioned
    179 Post(s)
    Tagged
    9 Thread(s)
    Hi,

    Well you use try ... catch when doing something that could go wrong, so that you can either ensure the operation completes successfully, or catch the error.

    Something like this:

    PHP Code:
    <?php

    // Get $_POST variables
    $header $_POST['header']; 
    $content $_POST['content']; 
    $footer $_POST['footer']; 

    $status"";

    function 
    get_db_connection( ){  
      
    // db connection stuff here
    }

    try {  
      
    $conn get_db_connection();  
      
    // insert header, content & footer into db here
      
    $status"success"
    } catch (Exception $e) {  
      
    $status"faliure"
    }

    echo 
    $status;
    ?>
    I didn't test this.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •