get value DIV in PHP - php

I want to get DIV contents in PHP to submit a form.
<div id="hello"></div>
In my search, I was advised to use <input type="hidden" but I do not know how to do.

If you are obtaining the HTML as a string, or even if its a remote web page, you will need a HTML parser. There are lots of these for PHP and the question has already been asked and answered. How do you parse and process HTML/XML in PHP?
If you are looking to do this on click or some other page event when a user does something, you need to use javascript instead of PHP. PHP is a server side language and cant be used to access elements in the users web browser, you need to use javascript for this.

If you don't want to use an input field to show a value (perhaps because you don't want the user to modify it or for styling reasons). You can display it in a div, and pass the value with the form submission by duplicating the value in a hidden input positioned inside your form. The div may be inside or outside your form.
<div id="user">Jaber</div>
<form>
... any other form fields
<input type="hidden" name="user" value="Jaber">
... submit button
</form>

Related

PHP, need refresh current page with additional information from the server, how?

I have a html/php page, which reads file NAMES on the server, collects user inputs, and returns results upon user clicking "submit" button. The returned results will be parsed and displayed in a table in the page. Certain functions are implemented in javascript, btw.
That's how the html/php usually works. Now I need another field that will allow user to specify a name, and I need "refresh" the page with additional info which will be read from file NAMES on the server. Let me call this ShowExtraName function, for the sake of wording.
Before ShowExtraName, I have html/php code like this (sorry for the format, not sure how to write html tags in the post):
<html>
<head>
<javascripts added here>
<table>
<div>
<form>
<continue html layout here>
<checkboxes with php reading file NAMES from server --- line XXYY>
<submit>
<php script to check user input and query server, then display returns in table. This section is blank before submit since certain input fields are not "isset".>
Now my question is how/where I can add the ShowExtraName. I believe it needs to be php, because I need determine not only user-specified name, but also get additional and related information from the server and display it in line XXYY.
Hope I describe it clearly. Thank you for your input!
You have to use JavaScript/jQuery and AJAX calls in order to be able to ask the server to execute some code and then to render the result back to the HTML without refreshing the page.
jQuery is a very simple and yet very powerful tool.
You can do this a number of ways.
Using only html and php, the easiest would probably be to use a form that uses GET with an action of the same page. Then check for get parameters in your PHP and print the extra row of data in your code.
<FORM action="" method="get">
<INPUT type="text" id="name"><BR>
<INPUT type="submit" value="Send">
</FORM>
<?php
if (isset($_GET['name'])) {
//do database query
//echo more stuff
}
?>
If you are doing a database query be sure to use prepared statements
Otherwise if you want to get fancy with a live refresh you will need to use JavaScript. (jQuery AJAX)

How can I make a form post to a url determined by the value selected in drop down?

I'm writing a simple PHP program with a form that has multiple fields to be filled in.
Among the fields is a <select> box.
I want the same form to post to 3 different URLs, determined by which of the three values have been selected in that <select> box.
How do I code something like this in PHP (with a typical HTML form)?
Any code samples/guides would be helpful.
I (being a newbie) am especially confused about this part: The form opening tag already specifies the target URL, so how can I change this later, depending on the input given by user in the drop down box, as the drop down box's HTML code is after the opening form tag(which already declares the form's target URL)?
You can use JavaScript like this:
<form method="get" action="http://example.com/url1">
<select name="_" onchange="this.form.action=this.value">
<option value="http://example.com/url1">URL1</option>
<option value="http://example.com/url2">URL2</option>
</select>
<input type="submit" value="Submit" />
</form>
Whenever the value in <select> changes, the form's action is updated to the required URL.
You can't do this in PHP. You must dynamically alter the form's action attribute with JavaScript to point to the URL you wish the form to submit to.
This can only be done in JavaScript, as only JavaScript has access to the client's browser where the form is being interacted with.
The alternative would be to have your form always submit to the same URL, and handle the submitted data with branching logic in PHP. I would prefer this method, as there is no requirement for JavaScript on the client.
With javascript you could grab the value of the drop down option selected and populate the form's action attribute.
With php, you could have one "results.php" page that could repackage the $_POST vars and send to the correct URL based on the $_POST['send_to_this_url'] var.
I'd do the JS version.

How to load database content using Select box without button & javaScript

I'm making an administrative page for a website, for editing front-end contents. I don't want to use JavaScript, I want to make it with raw PHP.
The thing is: The user will choose a page from the select box, and the contents according to the page will be loaded into two text boxes. Then the user will edit them on text boxes and update the contents using an update button.
(there is no submit button for the select box, I want 'onselect' data content load)
I need, the user will simply select a page, no button to be pressed then and the content will be loaded. I want not to deploy JavaScript for this. Is it possible using raw PHP? Any suggestion?
PHP is a server side programming language. You can't really affect the client side directly, only indirectly through forms etc.
If you accept your users may have to use the enter key, which will submit the form, you're fine.
But you can't have an action occurring on a select, without even using the enter key, without javascript.
Note that the requirement to not use javascript, especially for an administrative tool, doesn't make sense.
Yes, it's possible with raw PHP, but I do not recommend it.
You can put the select-box inside a
<form action="targetFile.php" method="GET">
To submit the form without a submit-button, you can use:
<select name="sel" onChange="this.form.submit()">
When the form is submitted, the browser loads the site "targetFile.php"
On this site, you can use php to set the text-box content:
<input type="text" value="
<?php
if($_GET['sel'] == 0) echo "content1";
else echo "content2";
?>
">
If you want to load the content dynamically (without realoading a site) you have to use Javascript and maybe Ajax.

HTML form within another form

I am new to html, I would be really glad if you can help me with this.
I have a web page where there is a list and some other text inputs and buttons. This option list can be populated by clicking the "add" button in the page, this add button is to direct to another page and in that page there are some chekboxes, those which are checked are loaded back to the main page,(where I have the list) .
At the end data in the main page needs to be loaded to the database, (what is in the list and in the text inputs).
Since I'm new I just know little about php and html, I thought I should have a form within a another form(form to "add items", form to load to the database) and it is not possible in html. Can anyone suggest the best way to do this? Do I need to use javascript?
Why can't the extra inputs (the ones that would be in the second form) be part of the first form? I think the question will become clearer if you post a sample form so we can see the relationship between the two forms.
But overall, since you're ultimately only submitting one form, then maybe all the inputs belong together. If what you're calling the second form isn't supposed to be visible right away, you can still have it be part of the same form, but only reveal it when needed.
Again, some sample data would help to understand the exact context of your question.
in php if you use input name="somename[]"
for a number of input elems
you will get an array in $_POST['somename'] and access all the values.
I think what you're after - if I understand you correctly - is ajax. Ajax allows you to asynchronously send data to/from another script without leaving the current page. This is accomplished using JavaScript. In your case I think what you need to do is set an onclick event in JavaScript to a button:
<input type="button" onclick="javascriptFunction()">
You can read more about ajax here:
http://www.tizag.com/ajaxTutorial/ajaxform.php

JS/PHP: Who is responsible for generating the content?

Here's a situation which I have encountered, creating a form on the client side and using PHP to process. Here are some considerations
The PHP script generates the form and send to the client side. This is because of internationalization issues
The client side uses JavaScript to submit form; the ID of the form is hard-coded inside the JavaScript as it is generated by PHP. This means everytime the PHP code is updated, the JS must change.
The question here is, who should be dependent on who? Should the JS generate the form instead, so that the PHP script has to know the names of the form elements? OR should it be the other way round?
PHP should generate the Form + a hidden field with the ID of the Form.
then the javascript submits the form.
Thats how I would do it...
If the Form is being generated from the PHP Script, then it should be easy to (as #xXx suggested) have that script add the relevant ID to the form for later processing. Whether that ID is added as an "id" attribute for the "form" element, or as a hidden "input" field would be dependent on a number of factors.
The Javascript, rather than needing a hard-coded value within itself, should be configured to (if needed) find the ID within the form, as set by the PHP Script above.
Of course, this advice is a little airy-fairy as I have no idea how your solution has been designed.
But, for some visual aids:
In the PHP Script creating the Form
<?php
//After the Form open tag has been echo'd
//Assuming $formID is the Form's ID
echo '<input type="hidden" name="formID" value="'+$formID+'">';
?>
Which would create something like
<form ... >
<input type="hidden" name="formID" value="1234">
...
</form>
And then the Javascript should be able to do something like
//Assuming Javascript variable "targetForm" is pointing at the above form object
formID = targetForm.formID.value;

Categories