Recommend jQuery 1.6.4 ajax php form mail scripts?
I've been searching for script that fit jQuery 1.6.4 ajax php that I can use. Quite a lot that I've come across is part of script that is taking alot of time to put together. Is there anything that is clearly descriptive from start to finish. A form with check.php script too if that is possible.
I'm using a jQuery 1.6.4 form mail using ajax post with php script. A jQuery error message if a single input field is not filled.
To register a ID number and select module subject 1 or subject 2 with and submit. This will go to subjectData.mysql database
I've already designed a form mail using a jQuery 1.6.4.
<div class="field"><!-- Start Login-->
<form action="register.php" method="post" class="register">
<fieldset class="ui-widget ui-widget-content">
<label for="textinput1"><strong>Log in to register a module</strong></label>
<input id="textinput1" placeholder="people number" type="text" /></fieldset>
<fieldset data-role="controlgroup">
<input id="textinput2" placeholder="Password" type="password" /></fieldset>
<fieldset data-role="controlgroup" data-mini="true">
<input type="radio" name="radio-choice-1" id="radio-mini-1" value="modNo1" checked="checked" />
<label for="radio-mini-1">subject 1</label>
<input type="radio" name="radio-choice-1" id="radio-mini-2" value="modNo2" />
<label for="radio-mini-2">subject 2</label></fieldset>
<input type="submit" data-theme="a" name="submit" value="Register" />
<button data-theme="b" id="reset" type="reset">Reset</button>
</form>
</div>
<!-- END Login-->
</div>
I agree with prodigitalson, you need to know some php to write the code you need and build the response with php for the jquery ajax to receive it and do some javascript magic according to that response. But you have to code that custom bit.
The form will most likely send the POST data to check.php, where it will all be processed, and it will then build a json encoded respopnse to reply to the page with the form. You need then some javascript code on that page to:
Prevent default operation of the send button and bind to that click event the form submission with ajax.
Build the php code on check.php to do something with the POST data ($_POST array in PHP) and prepare a json encoded response (build an array and when done, echo the output of json_encodeing it).
build the javascript code to handle the ajax response from check.php
Some sample code on how to respond with php to a jquery ajax request here
Related
I have the following form in my HTML. The form is used to submit the data to a PHP file, which would then send notifications to android Phones. Now I would need an addition. I need to submit the same data to another PHP file, which would submit the data to another PHP page, which would send the notifications to Apple devices. I prefer to use two different PHPs for andorid and iOS. How can I place two buttons for the same form? The below is my HTML form.
<form action="notifdatabaseonlyios.php" method="post">
<label class="lb">Title</label>
<input type="text" name="title" class="form-control" />
<label class="lb">Description</label>
<textarea class="msg" name="desc"></textarea>
<br>
<input type="hidden" name="type" value='notification'>
<button class="btn btn-info pull-right">Submit</button>
</div>
</form>
one method:
Give 2 buttons in the form with different button names, If user clicks the first button go to notifdatabaseonlyios.php
if ($_POST['button1'])
{ }
else if($_POST['button2'])
{ }
?>
I've currently got the following set up:
I've got a form that submits an image, the action on that form calls to a new page
"test.php"
On test.php a function is called via a server request check
if ($_SERVER['REQUEST_METHOD'] == 'POST')
The problem with this current set up is that I have to load the page test.php. I want the user to stay on the same page after submitting the form.
I'm gessing that to achieve this I have to use some sort of Ajax call. But I'm not quite sure where to start. If I make an ajax call to the page test.php, will it still proces the server request method?
The form sends some coordinates, and the file test.php crops an image via these coordinates, and saves it to the server.
<form action="/test.php" method="post" onsubmit="return checkCoords();">
<input type="hidden" id="x" name="x" />
<input type="hidden" id="y" name="y" />
<input type="hidden" id="w" name="w" />
<input type="hidden" id="h" name="h" />
<input type="submit" value="Crop Image" class="btn btn-large btn-inverse" />
</form>
The coordinates are generated via another javascript function.
Any help or push in the right direction would help me out a lot.
Kind regards,
Use jQuery as it will make your life a lot easier.
Then, you can send a request like this:
$.post('ajax/test.php', function(response) {
$('#result').html(response);
});
This will send a POST request to ajax/test.php, then put the response output in a DIV with ID result.
The XMLHttpRequest object is used to exchange data with a server
But you can use ajax with jQuery to upload your images.
here is nice example :
Ajax Image Upload
Demo :
Ajax Image Upload Demo
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to determine if user selected a file for file upload?
I would like a simple PHP form validation. My form has two inputs: A link and a file to upload. The form should be submitted only if the link field is filled and the user selected a file to upload. If one of them is or both are false, an alert box should be displayed (like here when you click Upload without selecting a file).
This is my form:
<form action="upload_file.php" enctype="multipart/form-data" method="post">
<p>Link:<br>
<input type="text" name="link" size="50">
</p>
<p>Image:<br>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input type="file" name="file" id="file" size="40">
</p>
<div>
<input type="submit" name="upload" value="Upload">
</div>
</form>
I am new to PHP and I have been browsing tutorials for hours now, many of them was of no help or shows the server side validation.
I understand I should start with this:
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
to validate the fields before the form is submitted. I have some basic knowledge of PHP but I cannot figure out how to do this easy or not so easy task.
php is server side. What you need is a client side checker (js, jq ..)
You can perform the check only with php, but on your server, then return to the client the error message or the task complete message. If you want the check to be done client side, you need a client side language to handle it.
PHP is indeed a server-side application and thus cannot perform client-side validation.
If you really want to do client-side validation, you'll probably have to use javascript. Have a look at the jQuery validation plugin. Here is a big demo page showing you some of the possibilities: http://jquery.bassistance.de/validate/demo/
For more help and information, also have a look at the jQuery.com/plugins/validation page.
(Note the WARNING at the end of this post)
Example:
(the javascript)
<script>
$(document).ready(function(){
$("#commentForm").validate();
});
</script>
(the form)
<form class="cmxform" id="commentForm" method="get" action="">
<fieldset>
<legend>A simple comment form with submit validation and default messages</legend>
<p>
<label for="cname">Name</label>
<em>*</em><input id="cname" name="name" size="25" class="required" minlength="2" />
</p>
<p>
<label for="cemail">E-Mail</label>
<em>*</em><input id="cemail" name="email" size="25" class="required email" />
</p>
<p>
<label for="curl">URL</label>
<em> </em><input id="curl" name="url" size="25" class="url" value="" />
</p>
<p>
<label for="ccomment">Your comment</label>
<em>*</em><textarea id="ccomment" name="comment" cols="22" class="required"></textarea>
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
</fieldset>
</form>
WARNING
Please keep in mind that javascript, being client-side, runs on the client's computer. Don't rely on javascript validation alone. Anyone can disable javascript and insert 'wrong' values in your form.
In case of form it's best to do client side validation using some javascript library like jQuery. Check out this jQuery tool for form validation.
For server side validation, check out this link. It may help you.
Validation should be done on server side ALWAYS. You can add client minor validation also as a confort for the user with javascript.
Here some basic code to show a message if fields empty using minor validation:
<input type="submit" name="upload" value="Upload" onclick="var ref = document.getElementById('file'); if ( ref.length == 0 ){ alert( 'select file' ); return false; } ref = document.getElementById( 'textfieldid' ); if ( ref.value.length == 0 ){ alert( 'fill link, better use a regexp to match a url but this another store' ); return false; }">
For client side validation of "File upload" field, refer following plug-in :
http://adamsanderson.github.com/jQuery-File-Validator/
It is useful. I have tried it.
I have some problems when trying to pass some parameters to PHP via JS.
Here's the situation:
HTML:
<form id="numbtns" method="post" action="imphp.php">
<button id="1" onClick="pass(this.id)">1</button>
<button id="2" onClick="pass(this.id)">2</button>
<button id="3" onClick="pass(this.id)">3</button>
<button id="confirm" onClick="save()">submit</button>
</form>
JS:
pass(clicked_id) {
var btn = document.getElementById(clicked_id);
**// I have no idea what to do next to give the ids to the php file.**
}
save() {
**// I want function save() to collect the parameters from function pass(), and pass them to 'imphp.php' when submit button is clicked. I'm stuck here.**
}
PHP:
// connection part, let's skip it.
...
$num = $_POST['num'];
$query = sprintf("SELECT id, num FROM tb WHERE num = $num");
I know maybe it's a quick and stupid question for u, but I cannot figure it out for hours. So... could anyone help me out, I'd be very appreciate that.
Thx in advance.
I don't think you need Javascript for this. All you want to do is post the form to the PHP page, right?
<form id="numbtns" method="post" action="imphp.php">
<input type="radio" name="num" value="1" /> 1<br />
<input type="radio" name="num" value="2" /> 2<br />
<input type="radio" name="num" value="3" /> 3<br />
<input type="submit" value="Save" />
</form>
Using buttons made no sense to me at all, so I changed them to radio buttons. The user will check one, then click the Save button to submit the form.
You need to use AJAX... i would recommend simplifying the process with a javascript library like jQuery... here is a tutorial using jQuery / AJAX / PHP: http://www.php4every1.com/tutorials/jquery-ajax-tutorial/
This is a very frequently asked question in one of several forms, as you can see by looking under the Related heading to the right on this page.
I see this a lot and there seems to be a fundamental flaw in many people's understanding of how PHP and Javascript work, so I wanted to throw this out there.
PHP runs on the server, executing the code in the .php page and modules there, and produces output which is sent to the browser. PHP is now done. Stopped. Fini. You PHP code is no longer running.
The web browser receives the output, builds the DOM tree, and starts rendering the HTML as a page and executing the Javascript. The Javascript can't pass parameters to the PHP because the PHP is no longer running. It has terminated execution. Kaput. In addition, the Javascript is running in the browser, on the client machine, while the PHP runs on the server.
What you can do is invoke some new PHP (or re-invoke the same PHP code to do something else) and that is usually done be making an AJAX call from Javascript, as #Jeffery A Wooden suggests in his answer.
HTML, though I'd suggest changing the button elements to <input type="button" ...> elements
<form id="numbtns" method="post" action="imphp.php">
<input type="hidden" name="buttonID" value="1" />
<input type="button" onClick="pass(this)" value="1" />
<input type="button" onClick="pass(this)" value="2" />
<input type="button" onClick="pass(this)" value="2" />
<input type="submit" id="confirm" value="Submit" />
</form>
Javascript:
function pass(button) {
document.getElementById("buttonId").value = button.value;
}
How do you go about redirecting a browser and sending a HTTP POST request in PHP? A header("Location: file.php?foo=bar") of HTTP POST requests, if you will.
You can't - HTTP does not allow this - if you want to pass arguments via a redirect they have to be embedded into the URL as GET vars.
C.
This does not redirect the browser but it can perform a POST request.
Curl Manual
Curl POST Example
PHP POST Without Curl
To redirect the browser i'd suggest using Javascript.
An example form that does POST and redirect
<FORM action="http://somesite.com/prog/adduser" method="post">
<P>
<LABEL for="firstname">First name: </LABEL>
<INPUT type="text" id="firstname"><BR>
<LABEL for="lastname">Last name: </LABEL>
<INPUT type="text" id="lastname"><BR>
<LABEL for="email">email: </LABEL>
<INPUT type="text" id="email"><BR>
<INPUT type="radio" name="sex" value="Male"> Male<BR>
<INPUT type="radio" name="sex" value="Female"> Female<BR>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
</FORM>
I'm not sure why you would have any need for this, however it is not possible in any server-side language.
You could use a javascript library such as jQuery to request a page using a post request
I don't believe you can get a browser to POST data by redirecting it in the middle of a request. You're limited to GET. If you want a browser to POST something you need to construct a <form> and submit it. (Or use an AJAX request.)
PHP doesn't have anything like this. To fulfill your example, you can just simply say $_GET['foo'] = 'bar'; include("file.php"), however the URL given to the browser will not be changed.
Similar question: Code Translation: ASP.NET Server.Transfer in PHP
This question was asked here How do you POST to a page using the PHP header() function?.
Someone commented that if you already had the data why do you need to post it anywhere, why can't you just act on the data in that page?
If you need to transfer data when you redirect without showing data in the URL, you can use $_SESSION, first store data into session then redirect the page, after redirection get data from session and destroy the session data..
OK, if you need to transfer data to other site without showing in the URL, u have to use Javascript instead... like transfer data to payPal. just make a form and write a javascript code to submit the form automatically on page load. below is the sample code:
<form name="myform" action="handle-data.php">
Search: <input type='text' name='query' />
Search
</form>
<script type="text/javascript">
function submitform()
{
document.myform.submit();
}
</script>