adjust number of form rows - php

Have a query that should hopefully be nice and simple to answer.
I have a form that will be used to add rows into a database. The default number of rows to show when loading the page is 6. I would like to add the option to add more rows to the form, by changing a field, or clicking a button etc.
I currently use a while loop to print out the form. I simply say:
while($rows > 0) {
echo "FORM INPUTS HERE";
$rows = $rows - 1;
}
So the loop goes through a prints a set of inputs for record 1, and then takes one off the count, then prints a set of inputs for record 2, and so on.
Is there a way I can get the while loop to print more without refreshing the page? The simplest way would be to have a small form at the top of the page, with an extra columns field, and submit button. This would then post the value back to the same page, and add it to the default number of rows.
I would like to do this without having to submit, post or GET anything, but to have it happen then and there.
I think it may be tricky, as the while loop will have already run, and I dont know how to get it to run a second time.
Cheers
Eds

If you need an empty form, use Javascript and add this new form elements to the DOM on the fly! :)
Do you need to load some information on that form?

You can use ajax to make your wish. It doesn't refresh page, but sends out a request to the server asynchronously and fetches the content and appends to the form.
Example, if you have a code this way:
<form method="post">
<ul>
<li><label><strong>Field 1:</strong> <input type="text" /></label></li>
<li><label><strong>Field 2:</strong> <input type="text" /></label></li>
<li><label><strong>Field 3:</strong> <input type="text" /></label></li>
<li><label><strong>Field 4:</strong> <input type="text" /></label></li>
<li><label><strong>Field 5:</strong> <input type="text" /></label></li>
</ul>
</form>
Now using jQuery's $.getScript function, you can fetch a script, which is similar to this:
$("form ul").append('<li><label><strong>Field ' + $currentValue + ':</strong> <input type="text" /></label></li>');
This works out for you.

Thanks for all the suggestions guys.
I decided to just go for a small 1 filed form at the top of my page, where a user enters the number of rows they want to add, and then press the add button.
It should suffice, as the page doesnt NEED to be too fancy, and is much quicker and simpler to implement.
Thanks very much
Eds

Related

How to send get variables in the url without kill previous sent get variables?

Hello guys am trying to make a pagination for my website so when I get items back from the database I count the array items then I divide them on 9 cause I want always to display 9 on the page or less , to this I use the pagination links and send a get variable called "page".
now the question is how can I do the same thing when searching I mean when a user enter search keyword and then hit enter the search box will then send another get variable called "search" the problem it removes the first one "page"
I want my pagination to work for regular cases but also to work in combination with search results.
like when someone hit search the pagination changes to cycle through the search results instead of the whole web-store items .
example: examine this url :
localhost/webstore/store.php?page=1
now when user search for football for example the link would then be like this:
localhost/webstore/store.php?search=football
I want it to be like this
localhost/webstore/store.php?page=1&search=football
please help really important I should fix this today.
Thanks in advance.
Send page request in hidden format with the form, look at below code.
<form method="get" action="store.php">
<input type="text" name='search' value="" placeholder="Enter Keyword to search" />
<input type="hidden" name="page" value="1" />
<input type="submit" name="submit" value="search" />
</form>
this
<input type="hidden" name="page" value="1" /> will make your work done.
or
you can do one more way, if you are using ajax search, send query string manually by adding page=1
let's say you have query string http://localhost/webstore/store.php?search=football here add page=1 manually.
You can do that concatenate with your query string using + sign in javascript or jQuery
Hope it will serve your purpose ;)

HTML submit only submitting itself

first things first, english isn't my native language, so feel free to ask if I am being unclear.
I am currently trying to get a small webpage to work for a college task (and no, I'm not asking you to do my homework, but I currently am stuck and no amount of search has turned up valuable results so far) and it seems like my submit buttons only submit their own values and nothing else.
For example:
I have a form called "list" that has a select element, two buttons and one submit element.
Code:
<form id="list" action="process.php" method="post" onsubmit="return order()">
<select name="cart[]" id="myCart" size="6" multiple>
[contents of select element]
</select>
<p>
<input type="button" value="Delete All" class="custombuttonsmall" onclick="deleteElements()">
<input type="button" value="Delete Selected" class="custombuttonsmall" onclick="deleteElement()">
<input type="submit" value="Order" class="custombuttonsmall" name="order">
</p>
</form>
Note: the "order()" function checks if there are options in the select element. If there isn't, the process won't come through.
process.php currently only has two lines,
$q = $_POST;
var_dump($q);
to test if submitting works.
Result of var_dump:
array(1) { ["order"]=> string(5) "Order" }
Every other value I'm trying to call (e.g. $q = $_POST['cart']) returns NULL. Basically, my submit button seems to only submit its own value instead of the whole form. And I can't seem to figure out why. It happens for every form I'm trying t submit.
Sorry if this has been asked before or is too specific, but again, I haven't been able to get any progress on this so far.
Thanks in advance,
//EDIT:
Browsers used are Chromium 34.0.x and Firefox 30.0, same results on both.
Only selected options in a select element will be submitted.
Based on your bytton values, it seems likely that you are dynamically adding and removing options (without actually selecting them) instead of using the browser's native multiple selection UI.

Form refresh after submit

The form below can contains different elements of text fields, drop down and selection boxes, which allows the user to update his profile. The process of updating MySQL fields is being done after form submits.
<form method="post">
My Name: <input name="myname" type="text" value="<?php echo $_SESSION['SESS_MY_NAME']; ?>" /><br />
My Email: <input name="email" type="text" value="<?php echo $_SESSION['SESS_EMAIL']; ?>" /><br />
<input type="submit" name="Submit" value="Update" />
<?php
if (isset($_POST['Submit'])) {
// MySQL update
;
;
;
$result=mysql_query($sql);
}
// if successfully updated, make form refresh.
if($result){
;
;
;
}
?>
</form>
I want to refresh only the form, so that the user will stay in the same page and will see the changes that he made (i.e. What do I need to put under the comment in the code “if successfully updated, make form refresh”?).
I cannot use header("location: samepage.php"); because I have too many HTML codes involved in between and before.
Appreciate any help,
Move the isset($_POST['submit']) check to the top.
Your current (intended) process is as follows:
Retrieve form data from DB/Session/etc.
Display as HTML
User submits form
Repeat steps 1/2
Write data to DB
Force refresh
Repeat 1/2 again
If you move this check to the top, the process is changed to:
Retrieve form data from DB
Display as HTML
User submits form
Write data to DB
Repeat 1/2 (it will now retrieve the updated information and display correctly).
This is the absolute simplest way you can do it. It doesn't take into account Post/Redirect/Get or updating the session so multiple DB reads are unncessary, etc.
Why don't you use jQuery AJAX to submit and verify the form then output the result in chosen div element.
More on that available at nettuts+
Such helpful. Much wow.
Thankfully I was able to solve this on my own after some intense googling since no one else seemed to have an answer. For anyone looking to accomplish the same thing, all you have to do is add the following code to the "Additional Settings" section of your CF7 form configuration:
on_sent_ok: "location = 'http://domain.com/contact';"
Just replace the URL with the URL of your form page and presto, it'll automatically refresh back to that page when the form is submitted!

Submit same form to multiple locations without Javascript

Having looked at various similar questions, both on SO and elsewhere, I have a horrible feeling what I want to do is impossible, but here goes.
I have a page that is a table of text input rows. The user enters information on each row, and submits the data to a separate file, which creates a PDF.
The problem is that I need the user to be able to add rows to the table at will, since the amount of data can vary.
[Before you go there, I need to point out that I cannot use Javascript for any of this - I know it is easy to do in JS but the page needs to be accessible.]
Here is a very simplified version I just cobbled together to (hopefully) illustrate the point:
<?php
if (filter_has_var(INPUT_POST, 'add_rows')) {
$howmanyrows = filter_input(INPUT_POST, 'howmanyrows', FILTER_SANITIZE_NUMBER_INT);
//get all the data from table and put it in an array,
//then add 5 (or however many) new rows to said array.
}
else if (filter_has_var(INPUT_POST, 'send_data')) {
//get table data, add to session and redirect to other page with a header()
}
?>
<html>
<form action="" method="POST">
<table>
<?php //table rows added using an array of data
foreach ($data as $d): ?>
<tr><td><input type="text" value="<?php echo $d; ?>"></td></tr>
<?php endforeach; ?>
</table>
<input type="text" name="howmanyrows" value="5">
<input type="submit" name="add_rows">
<input type="submit" name="send_data">
</form>
...
</html>
As you can see, at the moment I have a clunky setup where there is just one form that encompasses the entire page, and submits the page to itself. Depending on the button that was clicked, a new row is added or the data is submitted to the PDF-creation page.
This is not ideal, for so many reasons. What I really want to be able to do is have two separate forms, or nested forms. But the former won't allow the input values to be submitted to both, and the latter is apparently bad form (no pun intended) and doesn't work.
Is it at all possible to make this do what I want it to do? Any suggestions for a different way to go about it?
I think you have the best non-javascript solution - certainly hte way I'd run with it.
One thing to make it easier is that you can use multiple inputs with the same name:
<input name="tablerow[]" type="text" value="A" />
<input name="tablerow[]" type="text" value="B" />
<input name="tablerow[]" type="text" value="C" />
And these come through the $_POST['tablerow'] as an array. The length of the array is the number of fields. Then add additional fields to that.
For accessibility, you should add a link at the top that allows the user to hop directly to the first "new" field - otherwise they need to tab through the entire form to get to the new field. (See my comment above about if JS is really unavoidable as you and they can avoid this scenario!)

Using javascript and $_SESSION's to record wanted user information over pagination

What I have so far is a paginated page, showing all users in a database system. Each page shows 30 users and has a checkbox next to each user, what I need is a way for users to select and deselect these users and for these selections to propagate through, so if the user goes back to page 1 from page 2, all users from page 1 will still be checked.
I also need a way to record this information, so that once the user has looked at all the pages and clicks a submit form all checked users information can be processed. I am thinking of using javascript to record the information and php sessions to store it, but with the way I am trying now, when a user clicks a checkbox, it is not ticked.
Does anyone have a better way of doing this/see how I can fix this problem?
Thanks.
<script type="text/javascript">
function log_export($str) {
document.check.data.value = $str;
document.check.submit();
}
</script>
<?php
if(isset($_POST['data'])) {
echo $_POST['data'];
}
?>
<form name="check" method = "post" action = "">
<input type="hidden" name="data">
<input type="checkbox" name="A" onclick="log_export('1')" />
<input type="checkbox" name="B" onclick="log_export('2')" />
<input type="checkbox" name="C" onclick="log_export('3')" />
</form>
Few ways you could do this, but I'd avoid using javascript to do it. You could use an array in your $_SESSION to keep the list across pages.
<form name="check" method = "post" action = "">
<input type="checkbox" name="person[A]" />
</form>
(note: The form elements are named person[A], person[B]...etc, so they can be accessed as an array in php and make your life easier.)
Then in the php you can store this in the session...
$_SESSION['saved_list'] = $_REQUEST['person'];
This way the session variable saved_list will contain the array person with all the checked boxes in it. You'll need to be careful not to overwrite the array each time however, so adding...
$_REQUEST['person'] = array_merge($_SESSION['saved_list'], $_REQUEST['person']);
...before this should keep them (if I'm remembering my merge functions correctly).
Alternatively, you could just use html to save the checkboxes already ticked. When page 2 receives the results from page 1, it could print them out as hidden elements at the end of the page 2 form. This way they can exist across pages, however this could become a bit unwieldy with 30 names a page.
I'd suggest storing it in a php session array, this shouldnt really involve using Javascript, it just over-complicates matters.
Why you don't change your form method to GET and make the pagination link so it contains every parameters passed to the form and the page number. I think it make everything more simple to handle that case with parameters passed on the URL against posted one.
you can create those links like this:
for ($i =0; $i < $max_page; $i++){
echo "{$_SERVER['REQUEST_URI']}?$_SERVER['QUERY_STRING']&p={$i}";
and you just have to change your backend to use $_GET instead of $_POST.

Categories