这个例子对我很好,你必须处理错误(空文本框)
例子:
必须包含 jquery.js html :
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<script src="jquery.js" type="text/javascript"> </script>
<script language="JavaScript" type="text/javascript" >
function get_url()
{
$.ajax(
{
type: 'POST',
url: 'gen.php',
dataType: "html",
data: {Name: $('#Name').val(), Company: $('#Company').val(),Title: $('#Title').val(), Phone_number: $('#Phone_number').val(),Email: $('#Email').val(), Address: $('#Address').val(),Address_2 : $('#Address_2').val(), Website: $('#Website').val(),Memo: $('#Memo').val(), Encoding: $('#Encoding option:selected').val(), Barcode_size: $('#Barcode_size option:selected').val(), Error_correction: $('#Error_correction option:selected').val(), Character_encoding: $('#Character_encoding option:selected').val()},
success: function(msg) {
$('#myurl').val(msg);
$('#img').attr('src',msg);
} });
}
</script>
<title>
</title>
</head>
<body>
<form action="javascript:get_url();">
<table cellspacing="0" cellpadding="0" id="mainpanel"> <tbody>
<tr>
<td align="left" style="vertical-align: top;">
<table id="leftpanel"> <colgroup> <col> </colgroup> <tbody>
<tr>
<td> </td>
</tr>
<tr>
<td>
<table> <colgroup> <col class="firstColumn"> <col class="secondColumn"> </colgroup> <tbody>
<tr>
<td class="firstColumn">Name
</td>
<td class="secondColumn">
<input type="text" id="Name">
</td>
</tr>
<tr>
<td class="firstColumn">Company
</td>
<td class="secondColumn">
<input type="text" id="Company">
</td>
</tr>
<tr>
<td class="firstColumn">Title
</td>
<td class="secondColumn">
<input type="text" id="Title">
</td>
</tr>
<tr>
<td class="firstColumn">Phone number
</td>
<td class="secondColumn">
<input type="text" id="Phone_number">
</td>
</tr>
<tr>
<td class="firstColumn">Email
</td>
<td class="secondColumn">
<input type="text" id="Email">
</td>
</tr>
<tr>
<td class="firstColumn">Address
</td>
<td class="secondColumn">
<input type="text" id="Address">
</td>
</tr>
<tr>
<td class="firstColumn">Address 2
</td>
<td class="secondColumn">
<input type="text" id="Address_2">
</td>
</tr>
<tr>
<td class="firstColumn">Website
</td>
<td class="secondColumn">
<input type="text" id="Website">
</td>
</tr>
<tr>
<td class="firstColumn">Memo
</td>
<td class="secondColumn">
<input type="text" id="Memo">
</td>
</tr>
<tr>
<td class="firstColumn">Encoding
</td>
<td class="secondColumn">
<select id="Encoding">
<option selected value="MECARD">MECARD </option>
<option value="vCard">vCard </option>
</select>
</td>
</tr> </tbody>
</table>
</td>
</tr>
<tr>
<td>
<span id="errorMessageID">
</span>
</td>
</tr>
<tr>
<td>
<table> <colgroup> <col class="firstColumn"> <col class="secondColumn"> </colgroup> <tbody>
<tr>
<td class="firstColumn">Barcode size
</td>
<td class="secondColumn">
<select id="Barcode_size">
<option selected value="120">Small </option>
<option value="230">Medium </option>
<option value="350">Large </option>
</select>
</td>
</tr>
<tr>
<td class="firstColumn">Error correction
</td>
<td class="secondColumn">
<select id="Error_correction">
<option selected value="L">L </option>
<option value="M">M </option>
<option value="Q">Q </option>
<option value="H">H </option>
</select>
</td>
</tr>
<tr>
<td class="firstColumn">Character encoding
</td>
<td class="secondColumn">
<select id="Character_encoding">
<option selected value="UTF-8">UTF-8 </option>
<option value="ISO-8859-1">ISO-8859-1 </option>
<option value="Shift_JIS">Shift_JIS </option>
</select>
</td>
</tr> </tbody>
</table>
</td>
</tr>
<tr>
<td>
<table> <colgroup> <col class="firstColumn"> <col class="secondColumn"> </colgroup> <tbody>
<tr>
<td class="firstColumn">
</td>
<td class="secondColumn"> <button type="submit" >Generate → </button>
</td>
</tr> </tbody>
</table>
</td>
</tr> </tbody>
</table>
</td>
<td align="left" style="vertical-align: top;">
<table cellspacing="0" cellpadding="0"> <tbody>
<tr>
<td align="left" style="vertical-align: top;">
<div id="imageresult">
<div id="innerresult"> <img id="img" src="" >
</div>
</div>
</td>
</tr>
<tr>
<td align="left" style="vertical-align: top;">
<div class="gwt-HTML" id="downloadText" style="">
embed with this URL:
</div>
</td>
</tr>
<tr>
<td align="left" style="vertical-align: top;">
<input name="myurl" id="myurl" type="text" class="gwt-TextBox" id="urlresult" style="">
</td>
</tr>
<tr>
<td align="left" style="vertical-align: top;"> </td>
</tr> </tbody>
</table>
</td>
</tr> </tbody>
</table>
</form>
</body>
</html>
php:
<?php
$post = (!empty ($_POST)) ? true : false;
if (!$post) {
die();
}
$data = '';
@ $Name = $_POST['Name'];
@ $Company = $_POST['Company'];
@ $Title = $_POST['Title'];
@ $Phone_number = $_POST['Phone_number'];
@ $Email = $_POST['Email'];
@ $Address = $_POST['Address'];
@ $Address_2 = $_POST['Address_2'];
@ $Website = $_POST['Website'];
@ $Memo = $_POST['Memo'];
@ $Encoding = $_POST['Encoding'];
@ $Barcode_size = $_POST['Barcode_size'];
@ $Error_correction = $_POST['Error_correction'];
@ $Character_encoding = $_POST['Character_encoding'];
$Barcode_size = $Barcode_size . "x" . $Barcode_size;
$data = 'http://chart.apis.google.com/chart?'; // url
$data .= 'cht=qr&chs=' . $Barcode_size . '&chld=' . $Error_correction . '&choe=' .$Character_encoding . '&chl=' . $Encoding . ':'; // required data
$data .= 'name:' . $Name . ';Company:' . $Company . ';Title:' . $Title . ';Phone_number:' . $Phone_number . ';Email:' . $Email . ';Address:' . $Address . ';Website:' . $Website . ';Memo:' . $Memo . ';;'; // your data -> name :value ; and at the end of the line -> ;
echo $data;
?>