I'm having problems submitting my ajax form. I am used to the old fashioned way with refresh but this new stuff is beyond me for the time being. It's time to start learning new technolohies.
I have an autosuggest box that is getting my results from a database and populating the textbox just fine. When I designed this about 6 months ago, I was searching the database on the value rather than the key value. This is a problem today and needs to be fixed.
WHat the ajax has returned to my script is the key/value pair. Now that I have the id, I need to pass that into my php method so I can process it from there.
Can somone please give me a hand with this? It seems simple enough but again, javascript was never my thing so I am lost.
Here is all of the relevant code. Also, I don't think, at least from the code samples I have seen so far that I even need a form tag. Am I correct on this? Ideally, I want to submit the found ajax value with the enter button and NOT using a button.
So, just to clarify, this is what happens. The user types 2 or 3 letters. The ajax queries the db on a "LIKE" operator and returns the matches. The user chooses the one he wants and then the id goes out to my method and returns the exact record in a different window.
<form method="post" class="hdrForm" id="search" action="../../index.php?cer=2" target="_top">
<input type="text" name="string" class="hdrInput" id="string" value="Quick Search"><div id="acDiv"></div>
</form>
Note.. I need the "id" in this function to be submitted. Right now, I am getting the POST val off the form tag and that's not correct but how?
AC.chooseFunc = function(id,label)
{
document.forms.search.submit();
}
Thanks for any help that you guys can give me on this.
Take a look at jQuery. It is a javascript library. It contains functionality for doing Ajax.
jQuery Ajax documentation.
document.getElementById("search").onsubmit = function() {
// Do what you want with the form
return false; // Stops submit continuing
}
This also degrades gracefully (if your server side program is written right) in that users without javascript get the form submitted normally to the page in the action attribute, without the AJAX.
I'd suggest you use a framework such as jQuery. A basic tutorial (including AJAX) is available
You have two problems. One is that you are telling the form to submit:
document.forms.search.submit();
That is what is causing your form to submit in the standard, non-xhr way - causing a refresh. Also, because your form does not contain an input element for the id, that is not being sent to the server even with a regular form submission.
I agree with the posters that it would be a good idea to use jQuery or something to do your ajax based submission. Something like this could be used inside of your "AC.chooseFunc" function instead of the form submit.
And yes, if you go ajax entirely, you don't even need a form tag.
Related
I am fairly new to using PHP so bear with me if this is a stupid question
I have a form that comprises a number of radio buttons, the action is set to redirect to the same page and the method is GET.
A click on a radio button gets data from the database. The data is used to redisplay the same page with changed content.
The page URL has PHP arguments in it like the example below
localhost/basesite/mypage.php?itemID=8&name=city&number=9
When I access the page and click on a radio button I get a page with “no arg” because the URL reads
localhost/basesite/mypage.php?number=6
Two of the arguments are missing and that the last one is incorrect.
With no change whatsoever to the code except using ”post’ instead of “get” the whole thing works flawlessly.
I have used
form action= "" method=“get”
form action= “#” method=“get”
and many other actions using $_SERVER["REQUEST_URI”], $_SERVER['QUERY_STRING'] etc and combinations thereof.
Those that worked with POST did not work with GET.
I do not need to use POST as data is not written only retrieved from the database so I have no worry about data being written more than once.
If I have to I will use POST but if the user refreshes or uses the back button then the usual warnings will be issued by the browser.
What am I missing?
you should you use $.get which is a jquery method.
First, you should share your full source code for better understanding your problem. And also you have to use post method to submit a radio button values to get some value from your database. Form data can be submitted using these two methods (get and post). Both are used for the same purpose, but stands apart under some specifications. As in GET method key values are passed in the Url while in POST, the information transfers in a hidden manner.
Sorry folks. It was a badly formed URL due to me not fully understanding how to set a hidden element.
What I am trying to do is check, if any forms on the page are curently being filled out before a timed refresh. At this point I have 2 forms on my page posts and post replies and dont want that to get interupted. I want to keep the page current as close to real time as possible for incomming messages feeds ect. I've tried a few ways
a:
if($_SERVER['REQUEST_METHOD'] == 'POST')
b:
if(isset($_POST['POST'])
but these just check after the form is submitted.
To check form field is filled before submit, it can be done by using ajax or jquery. But jquery is more faster option to have this. Remember that , user can easily false script on run time so for better practice use both validations. For jquery simple use if($('selector').val()!=""){$('form').submit();}
First things first- PHP and HTML5 are not going to help you to get real-time data. You need javascript.
Since your question is too broad, I'd suggest you to get your hands dirty with javascript first. And then get back to your original project. Good luck!
There are far too many ways to solve your problem. It depends how much you can take and what way suits best for your website.
Use javascript to fix this, you can't get this done in php.
Don't do a timed refresh on the entire page. Only refresh parts of the page and leave the forms intact.
For instance, if you use jQuery, use .load() into a div with a timeout to refresh.
It's certainly client based job i.e JavaScript (JQuery) you can't do it through a server based language i.e PHP..!
You can't workout anything with PHP without hyperlinks but for some instance you can workout some stuffs with use of Ajax i.e to check if current filled input data is already present in the database without submitting the form manually everytime when you stop typing it will check database for the entered data...!
Ok I would like to say thanks again! I was trying to avoid javascript so I could better absorb html and php but it seems like javascript is a necessity esspecially in the social networking type site that I had to do for my first web design project. I spent most of the day going over javascript syntax and functions and for my issue of checking if a textbox is empty I just made a simple function
html:
<textarea id= 'icon' name = 'post_' placeholder='Make a post'wrap='hard'spellcheck='true' lang='en' style='background-image:url($ico); resize: none;padding-left: 55px' required title='Make a post.' onmouseleave='CheckInput(this);'></textarea>
javascript:
<script>
function CheckInput(input)
{
if (input.value == '')
{
alert("no input");
return false;
}
alert("yes input");
return true;
}
</script>
I am creating a button as such
<INPUT type='submit' name='name-of-button' value='Submit'>
However, this button is NOT enclused in a FORM. In other words, there isn't any form to submit.
That said, when a user clicks on the button, a certain action should start in PHP. For the sake of an example, clicking on Submit will write a value to the database.
Now, I am not sure how to handle the code in PHP. Do I have to put a form around it? I would like to check using something simlar to following PHP?
if (isset('name-of-button')) {
// Do something, such as writing to the db
}
I know the above code is incorrect. Because we have to submit the form, and use the post action as follows:
if (isset($_POST['name-of-button'])) {
// Do something, such as writing to the db
}
But again, I do not like to use a form if possible. PLEASE HELP :)
Please not that I do not want to use JavaScript either, pure PHP.
Thanks.
You can't make a browser send data to the server with only PHP and a HTML button. You have to make either a link that looks like a button, use JavaScript (AJAX), or enclose the button in a form.
OK, thanks everyone for your replies.
Just to make sure the post is complete and to help others:
As stated by some of your answers, to call a PHP function, I must either use form submit, use AJAX, or use a link. Therefore, for me, I will choose the easiest solution, which is use a form.
That said, according to the standard in HTML, you can use INPUT tags without a form. I think that was done so as to handle AJAX cases where you do not need a form. I was trying to find a way of doing the same straight to PHP, without a form, and without AJAX. It oviously does not work.
Thanks.
Firstly, let me say that I've been searching the Internet, rewriting scripts and I still can't seem to find a solution to my problem or even one that could possibly help. I've even searched this site for similar questions and the focus seems to be on actually displaying the database values but what I want is to access those values. Or rather let me put it like this, I have a drop down that is generated through php and it contains data retrieved from mysql... all that works fine no errors.
My current problem is that when a user select an option from my dropdown, I want to use that value to do more operations based on the user's selection. Please help, I've been at it for a week with no avail. Thanks in advance. If you need samples of my code please tell me.
Your problem is not that easy since php only runs on the server and does not directly get any information about what a user is clicking and selecting in his browser.
One possible solution (and the most common one nowadays) is to load any further information asynchronously via AJAX as soon as the user selects something in your dropdown. jQuery is a neat framework that can help you with this.
An other method is to automatically submit the form as soon as something is selected and read the selected value with php ($_POST variable). With this you could then easily generate a more custom website according to the selection that was made. Code could look like this:
<form method="post" action="" name="myform">
<select name="myfield" onchange="this.form.submit()">
<option .... >
...
</select>
</form>
(Note that the form is submitted when the onchange event is triggered)
This method clearly has the disadvantage that the whole page has to be reloaded whenever a selection is made. With the AJAX approach the user does not even notice that some additional information was fetched form the server. (Except a small waiting time that may occur)
What you want to do depends on your own decisioin. However the first approach may need you to get into some javascript programming. But the result is certainly more comfortable for the user...
You could make a callback function on the onselect Event. This is the easiest with jQuery in my experience.
http://api.jquery.com/select/
Wrap your select in a form which had a post of type 'get'. This will post to the same page you are in and then you can use the get parameter on page refresh to load your second list
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