Ajax调用 - 我需要一个Ajax调用,它将表数据发送到我的PHP查询

时间:2015-10-02 11:41:11

标签: javascript php mysql ajax

$client_number = $_POST['client_number'];
	$client_name = $_POST['client_name'];
	$service = $_POST['service'];
	$size = $_POST['size'];
	$volume = $_POST['volume'];
	$deliver_point = $_POST['deliver_point'];
	$port_orgin = $_POST['port_orgin'];
	$a_port_orgin = $_POST['a_port_orgin'];
	$road_freight = $_POST['road_freight'];
	$terms = $_POST['terms'];
	$competitor = $_POST['competitor'];
	$freight_speed = $_POST['freight_speed'];
	$report_comments = $_POST['report_comments'];
	$company_stage = $_POST['company_stage'];
	$meeting_rating = $_POST['meeting_rating'];
	$client_user_name = $_POST['client_user_name'];
	$client_user_status = $_POST['client_user_status'];
	$client_user_kids = $_POST['client_user_kids'];
	$client_user_hobbies = $_POST['client_user_hobbies'];
	$client_user_comments = $_POST['client_user_comments'];

	
						
	$query="INSERT INTO tobytemp.fcl_reports (client_number,client_name,service,size,volume,deliver_point,port_orgin,a_port_orgin,road_freight,terms,competitor,freight_speed,report_comments,company_stage,meeting_rating,client_user_name,client_user_status,client_user_kids,client_user_hobbies,client_user_comments)

	VALUES ('".$client_number."', '".$client_name."', '".$service."', '".$size."', '".$volume."', '".$deliver_point."', '".$port_orgin."', '".$a_port_orgin."', '".$road_freight."', '".$terms."', '".$competitor."', '".$freight_speed."', '".$report_comments."', '".$company_stage."', '".$meeting_rating."', '".$client_user_name."', '".$client_user_status."', '".$client_user_kids."', '".$client_user_hobbies."', '".$client_user_comments."');";
	
	$result = $dbLink->query($query);

<!DOCTYPE HTML>
<html>
<head>
<title>Sales App</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="shortcut icon" href="images/scicon.gif"/>
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(
hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
<link href='http://fonts.googleapis.com/css?family=Kreon:300,400,700' rel='stylesheet' type='text/css'>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
<!-- start plugins -->
<script type="text/javascript" src="js/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
	<script>
		$(function() {
			var pull 		= $('#pull');
				menu 		= $('nav ul');
				menuHeight	= menu.height();
			$(pull).on('click', function(e) {
				e.preventDefault();
				menu.slideToggle();
			});
			$(window).resize(function(){
        		var w = $(window).width();
        		if(w > 320 && menu.is(':hidden')) {
        			menu.removeAttr('style');
        		}
    		});
		});
	</script>
	

	

<!----font-Awesome----->
   	<link rel="stylesheet" href="fonts/css/font-awesome.min.css">
<!----font-Awesome----->
</head>
<body>
<div class="header_bg" id="home"><!-- start header -->
<div class="container">
	<div class="row header text-center specials">
		<div class="h_logo">
			<a href="index.html"><img src="images/logo.png" alt="" class="responsive"/></a>
		</div>
		<nav class="top-nav">
			<ul class="top-nav nav_list">
				<li><a href="fclclient.html">FCL Clients</a></li> 
				<li class="page-scroll"><a href="rates.html">Rates</a></li>
				<li class="logo page-scroll"><a title="hexa" href="index.html"><img src="images/logo.png" alt="" class="responsive"/></a></li>
			   <li class="page-scroll"><a href="clientreports.html">Reports</a></li>
				<li class="page-scroll"><a href="#contact">Contact</a></li>
			</ul>
			<a href="#" id="pull"></a>
		</nav>
		<div class="clear"></div>
	</div>
</div>
</div>



<div class="main_bg" id="about"><!-- start about us -->
<div class="container">
	<div class="row about">
		<div class="col-md-3 about_img">
			<!--<img src="images/captain.png" alt="" class="responsive"/>-->
		</div>
		<div class="col-md-9 about_text">
		
		
			</br></br><h3>Client Reports</h3>
			<h4>FCL Profile Extras</h4></br>
			<button class="buttons" id="showr">Show Table</button></br><button class="buttons" id="hider">Hide Table</button>
			</br></br>
			
								
<script>

$( "#showr" ).click(function() {
  $( "#fcl_form_data-table" ).slideDown( 100 );
});

$( "#hider" ).click(function() {
  $( "#fcl_form_data-table" ).slideUp( 100 );
});
</script>
			
			
			

			<table id="fcl_form_data-table" style="display: none">
			<th>BROSWER DB ID</th>
			<th>client_number</th>
			<th>client_name</th>
			<th>service</th>
			<th>size</th>
			<th>volume</th>
			<th>deliver_point</th>
			<th>port_orgin</th>
			<th>a_port_orgin</th>
			<th>road_freight</th>
			<th>terms</th>
			<th>competitor</th>
			<th>freight_speed</th>
			<th>company_stage</th>
			<th>meeting_rating</th>
			<th>client_user_name</th>
			<th>client_user_status</th>
			<th>client_user_kids</th>
			<th>client_user_hobbies</th>
			<th>client_user_comments</th>
			<th>Upload</th>
							
			
			
			
			<tr id="fcl_form_data-head">
			
			<!-- Saved data will be put here :D -->
			
			</tr>
			</table>
			
			</br>
			
		<!-- start -->
		
<div class="footer_bg">
<div class="container">
	<div class="row footer">
		<div class="col-md-8 contact_left">
		
			<form id="fcl_form_data-form" >
					
				<div class = "qborder" style="bottom-margin:500px; width:95%;">
				
					<h4>Client Info</h4></br>
					
					<input type="text" name="client_number" placeholder="Client Number" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Client Number';}">
					
					<input type="text" name="client_name" placeholder="Client Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Client Name';}">
				</div>
				
			<div class="multi-field-wrapper">
			  <div class="multi-fields">
				 <div class="multi-field">
							
							<div class = "qborder" style="width:15%;">
							
								<h4>Shipping Info</h4></br>

								<select name='service'>
								<option value=''>Service - </option>
								  <option value='Inport'>Inport</option>
								  <option value='Export'>Export</option>
								  <option value='Airfreight'>Airfreight</option>
								  <option value='W/housing'>W/housing</option>
								</select>
								
								</br></br>
								
								<select name='size'>
								<option value=''>Size - </option>
								  <option value='20'>20</option>
								  <option value='40'>40</option>
								  <option value='40'>40</option>
								  <option value='HQ'>HQ</option>
								  <option value='45'>45</option>
								  <option value='LCL'>Exact</option>
								</select>
								
								</br></br>
								
								<select name='volume'>
								<option value=''>Volume - </option>
								  <option value='Daily'>Daily</option>
								  <option value='Weekly'>Weekly</option>
								  <option value='Monthly'>Monthly</option>
								  <option value='Yearly'>Yearly</option>
								  <option value='Exact'>Exact</option>
								</select></br>
								
								<input type="text" name="deliver_point" placeholder="Deliver Point">
								
								<input type="text" name="port_orgin" placeholder="Port Origin">
								
								<input type="text" name="a_port_orgin" placeholder="A/Port Origin">
								
								<input type="text" name="road_freight" placeholder="Road Freight">
								
								<!-- onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Road Freight';}" -->
								
								<select name='terms'>
								<option value=''>Terms - </option>
								  <option value='EXM'>EXM</option>
								  <option value='FOB'>FOB</option>
								  <option value='PrePaid'>PrePaid</option>
								</select>
							
								</br></br>
								
								<select name='competitor'>
								<option value=''>Competitor - </option>
								  <option value='1'>1</option>
								  <option value='2'>2</option>
								  <option value='3'>3</option>
								</select></br>
								
								<input type="text" name="freight_speed" placeholder="Freight Speed"  onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Freight Speed';}">
								
								<textarea name="report_comments" placeholder="Comments"  onfocus="if(this.value == 'Comments') this.value='';" onblur="if(this.value == '') this.value='Comments;"></textarea>
							
						<button type="button" class="remove-field">Remove</button>
						
						<button type="button" class="add-field">Add field</button>				
							
							</div>		
								</br>
							
								
				 </div>	  
			  </div>
			</div>

				
				<div class = "qborder" style="width:95%;">
					<h4>Meeting Info</h4></br>			
					<select name='company_stage' >
					<option value=''>Company Stage - </option>
					  <option value='1'>1</option>
					  <option value='2'>2</option>
					  <option value='3'>3</option>
					  <option value='4'>4</option>
					</select>
					
					
						<select name='meeting_rating'>
					<option value=''>Meeting Rating - </option>
					  <option value='1'>1</option>
					  <option value='2'>2</option>
					  <option value='3'>3</option>
					  <option value='4'>4</option>
					  <option value='5'>5</option>
					</select>
				
				</div>
				
					</br>

				<div class = "qborder" style="width:95%;">
				<h4> Personal Information </h4></br>
				
					<input type="text" name="client_user_name" placeholder="User Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Name';}">
					
					<select name='client_user_name_status'>
					<option value=''>Status - </option>
					  <option value='Single'>Single</option>
					  <option value='Married'>Married</option>
					</select>
					
					<select name='client_user_name_kids'>
					<option value=''>Kids - </option>
					  <option value='1'>1</option>
					  <option value='2'>2</option>
					  <option value='3'>3</option>
					  <option value='4'>4</option>
					  <option value='5'>5+</option>
					</select>
					
					<input type="text" name="client_user_name_hobbies"  onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Hobbies';}">
					
					
					<textarea name="client_user_comments" placeholder="Comments"  onfocus="if(this.value == 'Comments') this.value='';" onblur="if(this.value == '') this.value='Comments;"></textarea>
				</div>
				
				</br>
				<span class="pull-left">
				
				
				

				
					
				<!-- <input type="button" id="fcl_form_data-op-discard" value="Discard" />	-->
			
				<input type="submit" id="fcl_form_data-op-save" value="Save" />
				
				

				<input type="hidden" name="id_entry" value="0" />
				
				
				
				
				
				</span>
				<div class="clear"></div>
			</form>
		</div>
	
	</div>
</div>
</div>

	<!-- End	-->
			
			
		</div>
	</div>
</div>
</div>




<div class="footer_bg" id="contact"><!-- start footer -->
<div class="container">
	<div class="row footer">
	


		<div class="col-md-4  contact_right">
			<!-- <p><span>About Us: </span> Established in 1998, The Cardinal Maritime Group is one of the fastest growing logistics service providers. </p> -->
			<ul class="list-unstyled address">
				<li><i class="fa fa-envelope"></i><a href="mailto:sales@cardinalmaritime.com">sales@cardinalmaritime.com</a></li>
				<li><i class="fa fa-phone"></i><span>England Tel: +44 (0) 161 492 1778</span></li>
				<li><i class="fa fa-map-marker"></i><span>Cardinal Maritime Limited, Leestone Road, Sharston Industrial Estate, Manchester M22 4RB </span></li>
			</ul>
		</div>		
	</div>
</div>
</div>
<div class="footer1_bg"><!-- start footer1 -->
	<div class="container">
		<div class="row  footer">
			<div class="copy text-center">
				<p class="link"><span>&#169; Cardinalmaritime | <a href="http://www.cardinalmaritime.com/"> Cardinal Maritime</a></span></p>
				<a href="#home" id="toTop" style="display: block;"><span id="toTopHover" style="opacity: 1;"> </span></a>
			</div>
		</div>
	</div>
</div>

<script>
		$('.multi-field-wrapper').each(function() {
			var $wrapper = $('.multi-fields', this);
			$(".add-field", $(this)).click(function(e) {
				$('.multi-field:first-child', $wrapper).clone(true).appendTo($wrapper).find('input').val('').focus();
			});
			$('.multi-field .remove-field', $wrapper).click(function() {
				if ($('.multi-field', $wrapper).length > 1)
					$(this).parent('.multi-field').remove();
			});
		});
			
	
</script>



<script>

var fcl_form_data = {
	index: window.localStorage.getItem("fcl_form_data:index"),
	$table: document.getElementById("fcl_form_data-table"),
	$form: document.getElementById("fcl_form_data-form"),
	$button_save: document.getElementById("fcl_form_data-op-save"),
	$button_discard: document.getElementById("fcl_form_data-op-discard"),
	init:
	function() {
			// initialize storage index
			if (!fcl_form_data.index) {
				window.localStorage.setItem("fcl_form_data:index", fcl_form_data.index = 1);
			}

		// initialize form
		fcl_form_data.$form.reset();
		
		/*
		fcl_form_data.$button_discard.addEventListener("click", function(event) {
		fcl_form_data.$form.reset();
		fcl_form_data.$form.id_entry.value = 0;
		},
		true);
		*/
		
		fcl_form_data.$form.addEventListener("submit", function(event) {
		var entry = {
		id: parseInt(this.id_entry.value),
		client_number:this.client_number.value,
		client_name:this.client_name.value,
		service:this.service.value,
		size:this.size.value,
		volume:this.volume.value,
		deliver_point:this.deliver_point.value,
		port_orgin:this.port_orgin.value,
		a_port_orgin:this.a_port_orgin.value,
		road_freight:this.road_freight.value,
		terms:this.terms.value,
		competitor:this.competitor.value,
		freight_speed:this.freight_speed.value,
		report_comments:this.report_comments.value,
		company_stage:this.company_stage.value,
		client_user_name:this.client_user_name.value,
		client_user_name_status:this.client_user_name_status.value,
		client_user_name_kids:this.client_user_name_kids.value,
		client_user_name_hobbies:this.client_user_name_hobbies.value,
		client_user_comments:this.client_user_comments.value
		};
		
		if (entry.id == 0) { // add
		fcl_form_data.storeAdd(entry);
		// Adds data to table when pressed save - like a temp table above
		fcl_form_data.tableAdd(entry);
		}
		else { // edit
		fcl_form_data.storeEdit(entry);
		fcl_form_data.tableEdit(entry);
		}
		this.reset();
		this.id_entry.value = 0;
		event.preventDefault();
		},
		true);
		
		////////////////////////////////////////////////////////////////////////////////////////Load DB info into table 
		
		if (window.localStorage.length - 1) {
			var data_list = [], i, key;
			
			for (i = 0; i < window.localStorage.length; i++) {
				key = window.localStorage.key(i);
				if (/fcl_form_data:\d+/.test(key)) {
					data_list.push(JSON.parse(window.localStorage.getItem(key)));
				}
			}
			if (data_list.length) {
				data_list
				.sort(
				function(a, b) {
				return a.id < b.id ? -1 : (a.id > b.id ? 1 : 0);
				})
				.forEach(fcl_form_data.tableAdd);
			}
		}
		
		/////////////////////////////////////////////////////////////////////////////////////
		
		fcl_form_data.$table.addEventListener("click", function(event) {
			var op = event.target.getAttribute("data-op");
			if (/edit|remove/.test(op)) {
				var entry = JSON.parse(window.localStorage.getItem("fcl_form_data:"+ event.target.getAttribute("data-id")));
						
				
				
				if (op == "edit") {
	
				fcl_form_data.$form.client_number.value = entry.client_number;
				fcl_form_data.$form.client_name.value = entry.client_name;
				fcl_form_data.$form.service.value = entry.service;
				fcl_form_data.$form.size.value = entry.size;
				fcl_form_data.$form.volume.value = entry.volume;
				fcl_form_data.$form.deliver_point.value = entry.deliver_point;
				fcl_form_data.$form.port_orgin.value = entry.port_orgin;
				fcl_form_data.$form.a_port_orgin.value = entry.a_port_orgin;
				fcl_form_data.$form.road_freight.value = entry.road_freight;
				fcl_form_data.$form.terms.value = entry.terms;
				fcl_form_data.$form.competitor.value = entry.competitor;
				fcl_form_data.$form.freight_speed.value = entry.freight_speed;
				fcl_form_data.$form.report_comments.value = entry.report_comments;
				fcl_form_data.$form.company_stage.value = entry.company_stage;
				fcl_form_data.$form.meeting_rating.value = entry.meeting_rating;
				fcl_form_data.$form.client_user_name.value = entry.client_user_name;
				fcl_form_data.$form.client_user_name_status.value = entry.client_user_name_status;
				fcl_form_data.$form.client_user_name_kids.value = entry.client_user_name_kids;
				fcl_form_data.$form.client_user_name_hobbies.value = entry.client_user_name_hobbies;
				fcl_form_data.$form.client_user_comments.value = entry.client_user_comments;
				fcl_form_data.$form.id_entry.value = entry.id;
				}
				else if (op == "remove") {
					if (confirm('Are you sure you want to remove "'+ entry.client_number +' '+ entry.client_name +'" from your fcl_form_data?')) {
					fcl_form_data.storeRemove(entry);
					fcl_form_data.tableRemove(entry);
					}
				}
				event.preventDefault();
			}
		},
		true);
	},
	storeAdd:
	function(entry) {
		entry.id = fcl_form_data.index;
		window.localStorage.setItem("fcl_form_data:index", ++fcl_form_data.index);
		window.localStorage.setItem("fcl_form_data:"+ entry.id, JSON.stringify(entry));
	},
	
	storeEdit:
	function(entry) {
		window.localStorage.setItem("fcl_form_data:"+ entry.id, JSON.stringify(entry));
	},
	
	storeRemove:
	function(entry) {
		window.localStorage.removeItem("fcl_form_data:"+ entry.id);
	},
	
	tableAdd:
	function(entry) {
	var $tr = document.createElement("tr"), $td, key;
	
		for (key in entry) {
			if (entry.hasOwnProperty(key)) {
			$td = document.createElement("td");
			$td.appendChild(document.createTextNode(entry[key]));
			$tr.appendChild($td);
			}
		}
		
		$td = document.createElement("td");
		$td.innerHTML ='<a data-op="edit" data-id="'+ entry.id +'">Edit</a> | <a data-op="remove" data-id="'+ entry.id +'">Remove</a>';
		$tr.appendChild($td);
		$tr.setAttribute("id", "entry-"+ entry.id);
		fcl_form_data.$table.appendChild($tr);
	},
	tableEdit:
	function(entry) {
		var $tr = document.getElementById("entry-"+ entry.id), $td, key;
		$tr.innerHTML ="";
		
		for (key in entry) {
			if (entry.hasOwnProperty(key)) {
			$td = document.createElement("td");
			$td.appendChild(document.createTextNode(entry[key]));
			$tr.appendChild($td);
			}
		}
		$td = document.createElement("td");
		$td.innerHTML ='<a data-op="edit" data-id="'+ entry.id +'">Edit</a> | <a data-op="remove" data-id="'+ entry.id +'">Remove</a>';
		$tr.appendChild($td);
	},
	tableRemove:
	function(entry) {
	fcl_form_data.$table.removeChild(document.getElementById("entry-"+ entry.id));
	}
};
fcl_form_data.init();
</script>


</body>
</html>

这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这里是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,这是我的完整脚本,

1 个答案:

答案 0 :(得分:1)

我建议你重新开始使用代码,这样你就可以更好地设计它,但是只要它已经太大了,而且我没有足够的时间,我和#39;保持你的风格,并尽力帮助你:

首先,您需要知道您的PHP代码只处理INSERT语句,因此,您需要为编辑(UPDATE语句)执行另一个代码,并且删除(DELETE声明)。

您已经拥有storeAdd功能,因此我已复制以创建databaseAdd一个:

databaseAdd: function(entry) {
  ajax('http://cmlsys/toby/fcl_form_upload.php', null, 'POST', entry);
},

然后,我们的ajax帮助者:

function ajax(url, callback, method, params) {
  if (!method) method = 'GET';

  var xhr = new XMLHttpRequest();
  xhr.open(method, url);

  if (callback) xhr.addEventListener('load', function() {
    callback.call(this, xhr);
  });

  if (params) {
    params = Object.keys(params).map(function (key) {
      return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
    }).join('&');
    xhr.send(params);
  }
  else {
    xhr.send();
  }    
}

因此,您将在storeAddtableAdd函数下方使用:

if (entry.id == 0) { // add
  fcl_form_data.storeAdd(entry);
  // Adds data to table when pressed save - like a temp table above
  fcl_form_data.tableAdd(entry);
  fcl_form_data.databaseAdd(entry);
}