I thought of making something that generates lots of form dynamically and then using jquery to handle their submission.
So i generate the form using php by using something like this.
while(some condition) {
echo "<form method=post action=specific_url.php name=some_form_name>";
echo "<input type=text>";
echo "<input type=submit>";
echo "</form>";
}
Well the above is just a skeleton. The main Problem is how should i name/id the form elements so that i can uniquely identify each form during submission and submit them, and use can extract the data at the specific_url page without any problem.
A direct analogy of this feature that i can think at the top of my head is that of facebook posts, where each post has got a comment box.
UPDATE
I guess i wasn't very clear with the question. Sorry about that. Let me rephrase it.
So till now, I have these dynamically generated forms. Now suppose user writes in one of the textbox and click on submit button.
At these point jquery should take control of it. Send that particular form data to the url. Retrieve the result and do something.
So in the end, I should be able to do something like this
$(some_selector).submit(function(e) {
e.preventDefault();
$.post('specific_url.php',$(some_selector).serialize(),function(data) {
// some stuff
});
});
Here "some_selector" is that selector which gets activated when a particular form is submitted and then sending that form's data.
You can use a hidden form element and set its value to an id for the form.
<input type="hidden" name="form_id" value="1">
This way you can access the form_id like any other form value once you submit it. I am looking at the facebook source now and I see a few hidden form elements next to the comment boxes
It is a requirement that the element id attribute is unique, not so with the name attribute.
It is also possible to use arrays of elements, for example :
<input name="comment[]" type=text>
<input name="comment[]" type=text>
<input name="comment[]" type=text>.
In your PHP. the value of $_POST['comment'] will be send as an array.
If you want to uniquely identify each comment in your PHP code, you will be forced to rename them uniquely.
<input name="comment1" type=text>
<input name="comment2" type=text>
<input name="comment3" type=text>
Since you are generating the form using PHP, this would be easy to keep track of the name that should be generated.
If you have multiple forms, you have free reign, sort of. There is no need a name for the form. However, to identify where in the page the form is places, place a hidden field.
while(some condition) {
echo "<form method=post action=specific_url.php";
echo "<input name='position_in_page' type='hidden' value='$somevalue'>";
echo "<input name='comment' type='text'>";
echo "<input type='submit'>";
echo "</form>";
}
Related
I have a really long form, which is basically a sign up for a college. And it's been working fine, although for some reason now I am not able to receive the POST data for the form anymore. I seem to receive all the variables, expect for the 'SUBMIT' button, which is what I look for to find out that it's posted.
It's a sliding form, meaning that it's about 450px high and it is split up into different 'pages', and at the bottom of each page there is previous, save for later, and next buttons. The previous and next activate jQuery functionality which slides the form left or right. The save for later button is a submit button which puts any received data into the database for retrieval later. The HTML for these buttons is:
<input type='button' class='previous' value='Previous'>
<input type='submit' class='saveforlater' name='saveforlater' value='Save For Later'>
<input type='button' class='next' value='Next'>
All three of these buttons work fine, if I test the receiving of saveforlater in PHP like so:
if(isset($_POST['saveforlater'])) {
// Do Stuff
}
It works every time.
However, right at the end of the form I have this:
<input type='button' class='previous' value='Previous'>
<input type='submit' class='next send' name='sendapplication' value='Send Application'>
And It used to work, but I cannot figure out what I've changed in order to stop it working. If I var_dump($_POST), I get all the other variables, except for this button.
So if I try to:
if(isset($_POST['sendapplication'])) {
I get nothing. NOTHING! I don't know why :(. It's such a simple thing and it's frustrating me.
If you click this button, you will see with var_dump or debug() returns this variable.
For submit buttons inside <form>, only buttons clicked will be setted as $_POST variable with its value.
Your input should have the attribute name. Your html should look like
<input type='button' class='previous' name='previous' value='Previous'>
<input type='submit' class='saveforlater' name='saveforlater' value='Save For Later'>
<input name='saveforlater' type='button' class='next' value='Next'>
Your php should look like in /enrol
<?php
var_dump($_POST);
Hope this helps you
How many fields are there in your form??
I think it is the problem due to size of limited post request. It is the problem of server configuration.
If you want to control it refer following link
What is the size limit of a post request?
If you're using a HTML form to send the data and a localhost server, your attribute action needs to have the PHP file directory on the server.
Im making my project, and my project is make a program that will enter name,age and address.
There was a 3 textbox and a submit button. when the user answer the 3 textbox and click submit button, the data or the input of the user will save into an array, but when the user try to input again the previous input will not be replace or it will be there together with the user new inputs, and later those input where use to save into mysql db, do you think this project is possible using php array? i have no sample code because i dont know how to start.thank you so much.
index.php
<form action='index.php' method='get'>
<input type='text' name='name[]'>
<input type='text' name='age[]'>
<input type='address' name='address[]'>
<input type='submit' value='Save'>
</form>
Simple solution is to use 'sticking form' - form elements that stick (keep) the values previously entered.
You can make a textbox sticky like this:
<input type='text' name='name' value='<?php isset($_POST['name'])? $_POST[\'name\']: ""; ?>' />
You need to sanitize the inputs, however, if you try to use it for some real application.
Is it possible in php to include a forms value into the action redirection?
For example:
<form method='POST' name='Select' action='customer.php?CID=xxxxx'>
<input type=text width='5' name='searchVal' />
where xxxxx is the value entered into the form.
I've tried a number of different ways and I'm just not figuring it out! (Still sort of new to php) Any help would be appreciated.
It was looking like I would have to use $_POST and $_GET. A little more information might be in order... customer.php displays a list of customers in order by ID, name, etc. The user currently clicks on the customer ID that they want to display the details for. I'm trying to add a box where they can just enter the customer number to get to the details quickly, but I still want to have the listing displayed. From what it is sounding like, I will have to do this as two separate programs...is that true?
How about this:
<form method='POST' name='Select' action='customer.php'>
<input type='hidden' value='xxxxx' name='CID' />
<input type=text width='5' name='searchVal' />
...
</form>
You are free to add as much hidden values as needed.
Note, that you can even use PHP-like array notation_
<input type='hidden' value='xxxxx' name='CID[1]' />
<input type='hidden' value='yyyyy' name='CID[2]' />
At the PHP-side, access those values using this syntax:
$_POST[ 'CID' ][ 1 ]
$_POST[ 'CID' ][ 2 ]
UPDATE-1
Ah, you want to use a user-entered values to the Action URL just before the form gets submitted?
In this case you need to use JavaScript. Access the DOM to change the Action URL.
But let me ask, why you need to post a form value additionally as a parameter of the Action URL?
UPDATE-2
You wrote: 'From what it is sounding like, I will have to do this as two separate programs...is that true?'
No, actually not. You can still use one customer.php which checks at its beginning, if it was called using a linked customer in the table element or a searched customer in the search field.
In other words: You don't need to prepare two scripts, but two forms for two purposes which call the same script customer.php.
You can include the required value in a hidden field in your form:
<input type="hidden" name="CID" value="xxxxx" />
The reason this is required is that you are submitting the form to your server via POST, but appending parameters to the URL requires submission via the GET method.
Not without a post to the server. The value in the form is filled in client-side, so it has to return to the server before you can add it to the action. (at least, if you want to use php).
You can either
add it after posting (might not be usefull)
use javascript
just not use the GET CID, but get it out of the POST in your customer.php script.
I got it finally! It's actually very simple!
In the body of the code I put this:
<form action='#_SELF' method='GET' name='Projected'>
<input type=text size=5 name='CID' value='' title='Enter Customer number to display' />
<a href='#' onclick='document.Projected.submit();' title='Enter Customer number to display'>Go</a>
And at the top of the code I just do a:
if (!isset($_GET['CID'])) { ...
It works exactly the way I wanted it to!
Thanks everyone for the help! I appreciate it! (And I'm learning more and more about PHP everyday!)
Im pretty sure you cant do that unfortunately
I have a form on a website I'm working (you can see it here) on that allows my users to give feedback. It's an jQuery/Ajax popup form:
$('.contact_us').click(function(){
var boxy_content;
boxy_content += "<div style=\"width:300px; height:300px \"><form id=\"feedbacked\">";
boxy_content += "<p>Subject<br /><input type=\"text\" name=\"subject\" id=\"subject\" size=\"33\" /></p><p>Your E-Mail Address:<br /><input type=\"text\" name=\"your_email\" size=\"33\" /></p><p>Comment:<br /><textarea name=\"comment\" id=\"comment\" cols=\"33\" rows=\"5\"></textarea></p><br /><input type=\"submit\" name=\"submit\" value=\"Send >>\" />";
boxy_content += "</form></div>";
// Other code here...
Is there anyway I can save what the user has entered in the form after they have clicked the close button and load it there next time the click the feedback button?
I would suggest that instead of recreating the whole boxy form on each click of the feedback button, create it once at the beginning and just show/hide it when you want.. this way it will maintain the contents of the previous submission..
Also do not rewrite its contents with the results of the ajax call.. create another box to be used for messages...
Why not have the form HTML and handling in a separate file, and just load it in using jQuery?
It should keep your code cleaner (no messy HTML in JavaScript), and you can easily implement a 'sticky' form by echoing the POST'ed data into the form values server-side.
UPDATE
Load the form into a dynamic iframe, so that when the user POSTs it, the whole page doesn't refresh.
I'm trying to make a page in php that takes rows from a database, displays them, and then give the viewer a chance to upvote or downvote a specific entry. Here is a snippet:
echo("<form action=\"vote.php\" method=\"post\"> \n");
echo("<INPUT type=\"hidden\" name=\"idnum\" value=\"".$row[0]."\">");
echo("<INPUT type=\"submit\" name=\"up\" value=\"Upvote.\"> \n");
echo("<INPUT type=\"submit\" name=\"down\" value=\"Downvote\"> ");
echo("<form/>\n");
The problem is when I hit a submit button, the value for idnum that gets sent is based on the one farthest down it seems. So my questions is, when a submit button is pressed, are the values for all inputs on a page sent?
Your form tag isn't closed properly. You have <form/>, but it should be </form>.
This makes the entire page a form so it sends all the inputs. With a form that is closed properly though, it will only send the inputs within the form tags that the pressed button was in.
While this wouldn't have helped with this particular issue, I would recommend not mixing your markup with your logic wherever possible. This is quite a lot more readable, and quite a lot more editable as well:
<form action="vote.php" method="post">
<input type="hidden" name="idnum" value="<?php echo $row[0]; ?>">
<input type="submit" name="up" value="Upvote.">
<input type="submit" name="down" value="Downvote">
</form>
Your form is not closed properly. Use </form> instead of <form/>.
The problem with your html is that you should have </form>, not <form/>.