I have a page that contains a table that the user can update. This has been done using javascript.
When the user has finised updating, I want to have the raw HTML page uploaded to the server, and then the user will be redirected to a php page which will do some final processing and spit out a report.
Is this possible or should I come up with another method of reaching the same end... and if so how do I get the dynamically created HTML onto the server to be manipulated?
You could have a form with a hidden input, and load the html into it using jquery.
So, your html may look like this:
<table id="table1">
... (this is the table from which you want to get the html)
</table>
<form id="form1">
<input type="hidden" id="table_html" name="table_html" />
<input type="submit" value="Submit" />
</form>
<script>
$("#form1").submit(function() {
$("#table_html").val($("#table1").html());
});
</script>
I didn't test this or anything, but the idea is that when you submit the form, the script populates the hidden value with the table html.
Now, if you have complex stuff in the table (I assume you do since the user can edit stuff right there), then maybe sending all the html isn't the best thing, you'll get a lot of garbage with the "useful" stuff. The way I'd do it is have the table inside the form, and have everything in inputs. Probably the names of the inputs will be like "first_name[]", so php will get arrays of values when there are several rows with the same cells and stuff like that.
Related
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>
For a website I'm currently building, I decided to let admin log in, and edit all of the source within each pages main content div. I'm using PHP and SQL to retrieve the source of the desired page (based on url). If a certain login session variable is set, and $_REQUEST["edit_page"] == true, the source of the content div is placed within a textarea, within a form.
This system really doesn't work if I try to add a form to the source. Anything outside of the pages source's form, displays as it would if the page wasnt being edited.
Is there any way around this?
EDIT:
Here's an example.
<form action="" method="post"><input type="hidden" name="edit_text" value="true"><input type="hidden" name="text_id" value="1"><input type="hidden" name="data_sent" value="true"><textarea name="new_text">
// SOURCE GOES HERE, LETS YOU EDIT IT, THEN SUBMIT IT, TO OVERWRITE THE CURRENT DATA STORED FOR THIS PAGE IN THE DATABASE
</textarea>
<input type="submit" value="Send">
</form>
This doesn't work if the source includes a form. It messes with the existing form, in which you are editing the source.
EDIT:
Basically, how do I display HTML source within a textarea, without it acting like HTML source?
Worked out you can use htmlspecialchars() in PHP to solve this. Problem solved.
You use htmlspecialchars() PHP function to display html source within textareas.
http://board.phpbuilder.com/showthread.php?10208990-display-html-source-in-text-area&p=10218525#post10218525
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)
Okay, I've looked for a solution to this question, but to no avail. It could be the way I worded it, but I've tried almost everything I could think of. And I know this is probably something so simple, I just can't wrap my head around it.
The problem I'm having is this:
I have a form that is prepopulated with data from a MySQL database. This part of the form is strictly for viewing the records (that were previously entered by the user). I can get it to work; I have the database linked, and it shows the data for the row of the particular table that I want. But this is what I'm having trouble with.
I want there to be a button that the user can press that cycles through each row of data in the database, and outputs it in the form accordingly.
It's a little complicated, so I'll use a simple example.
Let's say I have a basic table in a MySQL database with three columns: ID, Name,
and EmailAddress.
Here's the query that grabs the data:
$sql = "SELECT * from tbl_Users ORDER BY ID ASC";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);
Now I know this is deprecated, but I am working in an older version of php, and I'm trying to stay consistent other pages/apps in this particular domain.
Now let's say I have a simple form with two inputs and a submit button.
HTML
<form name="button" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="Name" value="<?php echo $row[Name]; ?>" />
<input type="text" name="emailAddress" value="<?php echo $row[EmailAddress]; ?>" />
<input type="submit" name="submit" value="Next Record" />
</form>
Now I can get it to echo what I need in the form. It can prepopulate the form with a row, as I want it to. The trouble lies with the submit button.
When the submit button is clicked, I want the next row to populate in the form, and so on until the end of the records.
I've tried the if(isset($_POST['submit']{...}, yet I don't know exactly to get this to work. I've tried loops, etc. I know there is a way, I just cannot comprehend one right now. Thanks in advanced.
Okay, so I DID manage to get it to work with PHP. I found a simple PHP Pagination script and changed the limit of the query to 1. Instead of echoing out the results of the query in the WHILE loop, I just called the variables for the form
Unfortunately in your case, PHP is server side, and it will run any queries before the client side has a chance to catch up and output it. I believe you will need to use javascript for this.
You can do one of two things, 1. Pull all table information on load (slower way) and use javascript to show/hide certain elements of that query. Or 2. You can use AJAX to pull the data on command.
I would suggest learning javascript (e.g. use a framework like jQuery) to perform an AJAX call for this.
I want to have a user select a file and then have php put the contents in db. Now the last part (processing the file in php) is easy. But is there a way I can process a user selected file whithout a new page load?
If I use the following:
<FORM ACTION="upload.php" METHOD="post" ENCTYPE="multipart/form-data">
<INPUT TYPE="file" NAME="somefile"><BR />
<INPUT TYPE="submit" NAME="submit" VALUE="Upload">
</FORM>
Page upload.php automaticaly loads after which I can insert the uploaded file in a database.
I would like to use a combination of javascript, php and xajax to process the file. I don't think something like this is possible:
<FORM ACTION="javascript:xajax_proces_file()" METHOD="post" ENCTYPE="multipart/form-data">
<INPUT TYPE="file" NAME="somefile"><BR />
<INPUT TYPE="submit" NAME="submit" VALUE="Upload">
</FORM>
Because the file is not uploaded when function xajax_process_file() is called. Or is it? I think I do not fully grasp the principle of uploads with javascript, html and php.
Any help and or clarification is much appreciated.
It may help to think of this as a two step process.
First, the user fills in the form and submits it - step one.
Second ( which is the default action ) the specified target file takes the input from the form and uses it to do whatever. You can almost think of a form "action" as a link - the default action of a link click is to display the result of the link. The same goes for a form action - display the result of a form action.
Now, it's possible via JavaScript to disable the default action of an element for a particular event. It is also possible via JavaScript to access a browsers HTTP mechanism to send/receive HTTP request (which is what every page request is - whether from your URL bar or a page link or a Google search result).
And that is what AJAX in simple terms is - using JavaScript to use a browsers HTTP mechanism to send requests to a web server and possible receive a response back without the use of a traditional click event. You then combine this with the use of JavaScript to "turn off" default actions and instead follow the action specified by you to get information from a server and add it to the page without ever having to refresh the page.
Many times to prevent the defualt action from taking place for a certain element, you return false in your code. The same goes for your form. Using javascript:
form.onSubmit = function() {
blah blah blah.....Use ajax to send the information to the form handler
return false; //Prevents the defualt action of the submit event
}
If you are really new to AJAX, I suggest you check out this tutorial and then this one. Lastly, I would recommend using a Javascript framework like jQuery to help you - it is awesome and does alot of great stuff, but also has easy and built in functionality for AJAX.
Here is another tutorial to do a form submit with no page refresh (uses jquery).
an alternative is to make the form directs the action to an iframe, after processing the query in the iframe, proceed by JS to clear the form of the father