#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Read Values from a html page


    This might seem really basic to some people but I don't know how to get the $_GET or $_POST to work when the php code is on the same page as the html form. I've seen
    'Form action = 'somefunc.php' but I don't want to call any outside function. I use jquery for tag clicking etc and in a small part of it I use some php where I need to base an if statement on the value of a text box but neither the $_GET or $_POST work. Is there some special way of identifying the form? Thanks in advance..
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,961
    Rep Power
    9397
    Before anything else, forms will submit to pages. Not to functions. The file may be named "somefunc.php" but the form will not be executing a function.

    To get $_GET and $_POST the form has to be submitted to the action (ie, somefunc.php) with the accompanying page refresh or AJAX. If you're doing stuff with Javascript then, well, it depends what you're doing.

    So what's your code?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    This is an excerpt of the code

    [code]
    <!DOCTYPE html>
    <html>
    <head>

    <!-- Javascript files -->
    <script type="text/javascript" src="<?php print $ThisLocation ;?>/Javascript/jquery-1.7.2.min.js"></script>

    <form action="" method="post" id="admin_options_form">

    <script type="text/javascript">

    $(document).ready(function()
    {
    $("#savedata").click(function () {
    $(this).hide("slow");

    <?php

    if ( stripslashes($_POST["clickPressed"]) == 'Yes')
    {
    update_option('myOption1',
    stripslashes($_POST['option1']) );

    update_option('myOption2',
    stripslashes($_POST['option2']) );

    update_option('myOption3',
    stripslashes($_POST['option3']) );
    }
    ?>

    });
    });
    </script>

    </head>
    <body>

    .....html text boxes etc

    The above $_POST's return a blank even though the values are there. I 'include this form in a different php function and the $_POST's work in that function. The values are read correctly there but I want them read within the form.
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,961
    Rep Power
    9397
    So you're saying that
    PHP Code:
    print_r($_POST); 
    always shows an empty array? And the form is most definitely being submitted?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    ok that is probably the problem. I didn't submit the form as I didn't know I had to. I'm assuming from your reply that I need to submit for $_GET and $_POST to work.
    Is there any way to read html values in that php section without submitting. I don't want to leave the page.

    Thanks for the help
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    I know I can read the html valuesin jquery but I want to access those values in the php code. Is it possible to use jquery within php like the following:

    [code]

    <?php

    if ( "<script>$("#myOption1").text</script>" == 'Yes')
    {
    .....
    }
    ?>
  12. #7
  13. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    311
    Originally Posted by NSOS
    I know I can read the html valuesin jquery but I want to access those values in the php code. Is it possible to use jquery within php like the following:

    [code]

    <?php

    if ( "<script>$("#myOption1").text</script>" == 'Yes')
    {
    .....
    }
    ?>
    no, php will treat that as a string, PHP as an interpretor / language does not know what javascript is.

    PHP is server side, the information you are trying to read is client side, so you can't access it directly with php, you need a way of sending the data to the server so PHP can do it's thing.

    You will need to do this via javascript / jquery. If you don't want the user to have to hit submit you can call a function when the user changes a field in the form.


    tldr: No, you cannot access a form with only php without the user hitting submit.

    -SD
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    thanks,
    you wrote -- 'If you don't want the user to have to hit submit you can call a function when the user changes a field in the form.' -- can that function (which I want to write in PHP) read the info on the form? if so how?
  16. #9
  17. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    311
    Originally Posted by NSOS
    thanks,
    you wrote -- 'If you don't want the user to have to hit submit you can call a function when the user changes a field in the form.' -- can that function (which I want to write in PHP) read the info on the form? if so how?
    you need to do some research into client side vs server side coding. I STRONGLY recommend you do that now. It will make your life a LOT easier.

    PHP Cannot see the data unless it is sent to it by either a user pushing the submit button, or javascript sends it.

    So the function which is called when a user updates a field in the form MUST be a client side language (basically, Javascript or JQuery). That JS function can then use AJAX to send the form data to a php script and wait for a response. That response can then be used to modify the page.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    thanks so much for the responses.
    I know I need to do a lot more research, it's just that in the last few months I've been learning HTML, CSS, jquery, some javascript, PHP and now it looks like I better learn some AJAX too. Just wanted to get this part finished and working before I tackle more.
  20. #11
  21. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    311
    depending on exactly what you are trying to achieve, it should be fairly straight forward. This should give you a good starting point: w3schools

    there are plenty of other good tutorials out there too.

    Comments on this post

    • requinix disagrees : w3fools.com
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    thanks, yes w3schools is a great place to learn. Thanks for all your help. I really appreciate it.
  24. #13
  25. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    311
    Originally Posted by NSOS
    thanks, yes w3schools is a great place to learn. Thanks for all your help. I really appreciate it.
    I wouldn't say a great place to learn, but it's good for a beginner to get a taste of what they need to do.

    requinex, I don't rate w3schools for indepth stuff, but for a first experience with a language they are not all bad.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  26. #14
  27. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,961
    Rep Power
    9397
    Originally Posted by sir_drinxalot
    I wouldn't say a great place to learn, but it's good for a beginner to get a taste of what they need to do.

    requinex, I don't rate w3schools for indepth stuff, but for a first experience with a language they are not all bad.
    For a glance at how things work, sure. But they offer more than that and a beginner won't know when to stop reading and start looking for more in-depth and accurate advice. PHP has enough of a problem as it is with things like magic quotes and register_globals; showing them how to echo $_GET values directly and embed POSTed values in SQL queries doesn't help.
  28. #15
  29. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    311
    Originally Posted by requinix
    For a glance at how things work, sure. But they offer more than that and a beginner won't know when to stop reading and start looking for more in-depth and accurate advice. PHP has enough of a problem as it is with things like magic quotes and register_globals; showing them how to echo $_GET values directly and embed POSTed values in SQL queries doesn't help.
    I fully agree, my intention was to open NSOS' mind to client vs server side and get them started on their way to using jquery to bridge that gap etc.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?

IMN logo majestic logo threadwatch logo seochat tools logo