SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2014
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems inserting values retrieved via AJAX into page

    Hello @pullo. I am jumping on this thread a bit after the fact and I really wanted to say thank you, or explanations have been an immense help. I have followed your examples here and I am able to observe that the javascript is calling a the sql procedure as defined in my code via sql profiler. However my index.php page is not loading the values as would be desired. Would you mind reviewing and making sure I am not missing something?

    index.php:

    Code:
    <html>
    	<head>
    		<title>Event Registration</title>
    		<link rel="stylesheet" type="text/css" href="css/master.css">
    	</head>
    	<body>
    		<div id="mainouter">
    <center>
    		<div id="mainheader">
    			<h2>Event Registration</h2>
    		</div>
    		<div id="maincontent">
    		<form method="post" action="submitreg.php" id="submitreg">
            Select an Event:
    			<select id="selectevent" name="eventid" form="submitreg">
    <?php
    	  $sqlserverName = "SQL2";
    	  $connectionInfo = array("Database"=>"eventtracker", "UID"=>"eventtracker", "PWD"=>"EventTracker77-", "ReturnDatesAsStrings" => 1);
    	  $conn = sqlsrv_connect( $sqlserverName, $connectionInfo);
    		  if( $conn )
    		  	{
    			  $sql1 = "{CALL USP_GET_EVENTS}";
    			  $stmt1 = sqlsrv_query( $conn, $sql1);
    			  if( $stmt1 === false ) {
    				  $stmt1response = "There was an error. Please Contact the IT Department";
    				  echo $stmt1response;
    				  die( print_r( sqlsrv_errors(), true));
    			  }
    		  } else {
    			  $connresponse = "Database Connection could not be established. Please Contact the IT Department";
    			  echo $connresponse;
    			  die( print_r( sqlsrv_errors(), true));
    			  }
    		while( $row = sqlsrv_fetch_array($stmt1, SQLSRV_FETCH_ASSOC) ) {
            echo   	'<option value="'.$row['evt_id'].'">'.$row['evt_nm'].'</option>';
    		}
    		sqlsrv_free_stmt( $stmt1);
    		sqlsrv_close($conn);
    ?>
    			</select>
    
    		<br /><br />
                <label for="firstname">First Name:</label>
                <input type="text" id="firstname" name="firstname" />
                <label for="lastname">Last Name:</label>
                <input type="text" id="lastname" name="lastname" />
    		<br /><br />
            	<label for="email">Email Address:</label>
            	<input type="email" id="email" name="email" />
    		Number of Guests:            
    <select name="guests" form="guests">
                	<option value"0">0</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>
                	<option value"6">6</option>
                	<option value"7">7</option>
                	<option value"8">8</option>
                	<option value"9">9</option>
                	<option value"10">10</option>
    </select>
    		<br /><br />
            <input type="submit" name="submit" value="Register">
    		</form>
    		<font style="color:#CED4DF;">All fields are required.</font>
    		</div>
            <br /><br />
            <div id="selectdetails">
    
    			Start Date/Time:<span id="star"></span>
                <br />
    			End Date/Time:<span id="fini"></span>
                <br />
    			Location:<span id="loca"></span>
                <br />
    			Other Info:<span id="othe"></span>
    
                <script src="beans/jquery.min.js"></script>
    	            <script>
    		            function insertResults(json){
    			            $("#star").val(json["evt_st"]);
    			            $("#fini").val(json["evt_ed"]);
    			            $("#loca").val(json["evt_loc"]);
    			            $("#othe").val(json["evt_oth"]);
    		            }
    		            function clearForm(){
    			            $("#star, #fini, #loca, #othe").val("");
                		}
    		            function makeAjaxRequest(eventid){
    			            $.ajax({
    			            type: "POST",
    			            data: { eventid: eventid },
    			            dataType: "json",
    			            url: "process_event.php",
    			            success: function(json) {
    						            insertResults(json);
    								 }
    			            });
    					}
    					$("#selectevent").on("change", function(){
    			            var id = $(this).val();
    						if (id === "0"){
    				            clearForm();
    			            } else {
    				            makeAjaxRequest(id);
    			            }
    		            });
                </script>
            </div>
    </center>
            </div>
    	</body>
    </html>
    And here is process_event.php:

    Code:
    <?php
    	$eventid = $_POST['eventid'];
    	$sqlserverName = "SQL2";
    	$connectionInfo = array("Database"=>"eventtracker", "UID"=>"eventtracker", "PWD"=>"EventTracker77-", "ReturnDatesAsStrings" => 1);
    	$conn = sqlsrv_connect( $sqlserverName, $connectionInfo);
    		if( $conn ) {
    			$sql1 = "{CALL USP_GET_EVENT_INFO}";
    			$stmt1 = sqlsrv_query( $conn, $sql1);
    				if( $stmt1 === false ) {
    					$stmt1response = "There was an error. Please Contact the IT Department";
    					echo $stmt1response;
    					die( print_r( sqlsrv_errors(), true));
    				}
    		} else {
    			$connresponse = "Database Connection could not be established. Please Contact the IT Department";
    			echo $connresponse;
    			die( print_r( sqlsrv_errors(), true));
    		}
    
    	$arr = array();
    	while ($row = sqlsrv_fetch_array($stmt1, SQLSRV_FETCH_ASSOC)) {
    		if ($eventid == $row['evt_id']){
    			$arr[] = $row;
    		}
    	}
    	echo json_encode($arr);
    
    	sqlsrv_free_stmt( $stmt1);
    	sqlsrv_close($conn);
    ?>
    Last edited by Pullo; Jun 27, 2014 at 00:50.

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Quote Originally Posted by mediaalchymia View Post
    However my index.php page is not loading the values as would be desired
    It would really help if I could see the page in question.

    Could you post a link?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •