Discuss Read Values from a html page in the PHP Development forum on Dev Shed. Read Values from a html page PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
Posts: 7
Time spent in forums: 1 h 47 m 55 sec
Reputation Power: 0
PHP-General - 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..
Posts: 12,717
Time spent in forums: 5 Months 1 Week 4 Days 7 h 29 m 55 sec
Reputation Power: 8969
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.
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.
Posts: 7
Time spent in forums: 1 h 47 m 55 sec
Reputation 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.
Posts: 7
Time spent in forums: 1 h 47 m 55 sec
Reputation 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')
{
.....
}
?>
Posts: 377
Time spent in forums: 1 Week 3 h 27 m 44 sec
Reputation Power: 293
Quote:
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
Posts: 7
Time spent in forums: 1 h 47 m 55 sec
Reputation 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?
Posts: 377
Time spent in forums: 1 Week 3 h 27 m 44 sec
Reputation Power: 293
Quote:
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.
Posts: 7
Time spent in forums: 1 h 47 m 55 sec
Reputation 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.
Posts: 12,717
Time spent in forums: 5 Months 1 Week 4 Days 7 h 29 m 55 sec
Reputation Power: 8969
Quote:
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.
Posts: 377
Time spent in forums: 1 Week 3 h 27 m 44 sec
Reputation Power: 293
Quote:
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.