I have a form on a page that is used to "Add/Edit a Location". Here is my form:
<form id='locationEditForm' name='locationEditForm'>
<table width='50%'>
<tr><td colspan='2'><hr></td></tr>
<tr><td colspan='2'><button type='submit' name='save' id='save'>Lookup Location By ID: </button>
<input type='text' size='10' name='locationToEdit'><br/><br/></td></tr>
<tr><td>Name:</td><td><input type='text' name='locationName'/></td></tr>
<tr><td>URL:</td><td><input type='text' name='locationURL'/></td></tr>
<tr><td>Coordinates:</td><td><input type='text' name='locationCoords'/></td></tr>
<tr><td>Address:</td><td><textarea name='locationAddress' rows='4' cols='40'></textarea></td></tr>
<tr><td>Phone:</td><td><input type='text' name='locationPhone'/></td></tr>
<tr><td>Hours:</td><td><textarea name='locationHours' rows='3' cols='40' wrap='soft'/></textarea></td></tr>
<tr><td colspan='2'><button type='submit' name='save' id='save'>Save Changes</button>
<button type='button' class="cancel">Cancel</button></td></tr>
</table>
</form>
I've wrapped the form elements in a table just for the sake of keeping the input boxes aligned for now in a "quick and dirty" sort of way.
Anyway, As you can see, I have a button and input area for the user to "lookup" an existing location by id. This is so that an existing location can be edited. I understand how to make that button 'submit' to my php code and to then lookup the location in the database, but my question is this -- is it possible to lookup the location in a MySQL database and load the information into the existing form without causing the page to reload as it normally would when I post the form?
Ideally, I imagine the data for an existing location just "popping" into the form without the entire page reloading. How would I go about doing something like this?
Thanks a ton!
The technique you're looking for is commonly referred to as Ajax. In your case, you'd want to submit an XMLHttpRequest() to a different PHP script which returns data you can use. Either HTML to insert into your form, or JSON data which you parse and use to update field values would be good choices.
There are lots of resources out there on how to make a PHP/MySQL endpoint for an Ajax request. These search terms should get you started. Good luck!
Indeed it is, you have to use javascript though. I would advise you to take a look at Jquery. And it's $.ajax and $.post functions.
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.
I still in training,
But i stuck in this problem.
// we need this password in 2 form POST
<input style='text' name='password' />
<form action='delete.php' method='POST'>
... // i need password to be here
<input type='submit' name='delete' value='Del' />
</form>
<form action='edit.php' method='POST'>
... // and i need password to be here too
<input type='submit' name='edit' value='Edit' />
</form>
My menthor want to do this using 2 form but I need the input password outside this 2 form.
Are there any way to do this without using script?
I can do this with 1 form and action.php that include 2 php file(edit.php and delete.php)
But he said that it is waste.
Can someone help? I already checking using search engine and found nothing.
Maybe your mentor just testing you on how you solve the situation.. As far as i know... you cant pass value of a textbox upon submit without putting the textbox inside a form.. I just wonder why javascript is forbidden.. Well anyway I suggest you to use 1 form and 1 page for the action and in the action make an if statement to separate you delete and edit method.. maybe in that case it is not waste.. It's just only a suggestion..
In PHP, i want to open more that one window and each one of them need to recieve different post data.
i know how to redirect a page via header location + Get DATA, but i really need to be able to send POST data.
EDIT:
Graph
|-Page with post 1
Main --|-Page with post 2
|-Page with post 3
so basically 1 page goes and open 3 pages with different post data on each. must be done server Side.
why not use three forms? ;-) Code tested, works.
<form method='post' action='http://<sever>/post1.php' id='action_frm1' name="action_frm1" target="_blank">
<input type='hidden' name='param1' value='hello'/>
<input type='hidden' name='param2' value='world'/>
</form>
<form method='post' action='http://<sever>/post2.php' id='action_frm2' name="action_frm2" target="_blank">
<input type='hidden' name='param1' value='hello2'/>
<input type='hidden' name='param2' value='world2'/>
</form>
<form method='post' action='http://<sever>/post3.php' id='action_frm3' name="action_frm3" target="_blank">
<input type='hidden' name='param1' value='hello3'/>
<input type='hidden' name='param2' value='world3'/>
</form>
<script type='text/javascript'>
function makePostRedirect() {
document.getElementById('action_frm1').submit();
document.getElementById('action_frm2').submit();
document.getElementById('action_frm3').submit();
}
makePostRedirect()
</script>
Of course, if you want post data via serverside usefull link will be this: http://noobflash.com/server-side-post-with-php/
you may use the following trick:
<form method='post' action='action.php' id='action_frm'>
<input type='hidden' name='param1' value='hello'/>
<input type='hidden' name='param2' value='world'/>
</form>
<script type='text/javascript'>
function makePostRedirect() {
document.getElementById('action_frm').submit();
}
</script>
You will need to do a workaround by calling window.open to some stub PHP pages that can then use some JavaScript to call a form post. What your asking can not really be done in PHP because PHP is server side and not front end. Your looking for a much more JavaScript reliant solution.
You can do a single POST to a new window by putting a target="_new" on the <form> element. It won't validate, but it will open ONE new window and submit the form data via that window.
For multiple windows, you'll have to hack together some JS to open the multiple windows, insert a form with a copy of the data you want to have posted in that window, then trigger the posts individually.
how about this:
Original page ---opens---> page 1 ---opens---> page 2 ---opens---> page 3
Each page passes the data for the remaining pages. Search for javascript pop-up code that works and runs on page open.
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'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/>.