Jump to content

php javascript beginner update mysql


PHP_CHILD

Recommended Posts

i have a form. if user enter the data, the db table display. but i want the table to get displayed when the user logins for the first time.

simple one but i can't get it how...

aa.html

<html>
<body>


<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code

function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

try{
 // Opera 8.0+, Firefox, Safari
 ajaxRequest = new XMLHttpRequest();
} catch (e){
 // Internet Explorer Browsers
 try{
  ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try{
   ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e){
   // Something went wrong
   alert("Your browser broke!");
   return false;
  }
 }
}


// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
 if(ajaxRequest.readyState == 4){
  var ajaxDisplay = document.getElementById('ajaxDiv');
  ajaxDisplay.innerHTML = ajaxRequest.responseText;
 }
}
var age = document.getElementById('age').value;
var wpm = document.getElementById('wpm').value;
var sex = document.getElementById('sex').value;
var name = document.getElementById('name').value;
var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex + "&name="  + name;
$a=ajaxRequest.open("GET", "ss1.php" + queryString, true);




ajaxRequest.send(null); 

}


//-->
</script>




<form name='myForm'>
Name:<input type='text' id='name' /> <br />
Age: <input type='text' id='age' /> <br />
WPM: <input type='text' id='wpm' />
<br />
Sex: <select id='sex'>
<option value='m'>m</option>
<option value='f'>f</option>
</select>
<input type='button' onclick='ajaxFunction()' value='Insert New row' />
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>

 

 

 

ss1.php

 

$age = $_GET['age'];
$sex = $_GET['sex'];
$wpm = $_GET['wpm'];
$name= $_GET['name'];
// Escape User Input to help prevent SQL Injection
$a=isset($_GET['age']) && $_GET['age'];
$b=isset($_GET['sex']) && $_GET['sex'];
$c=isset($_GET['wpm']) && $_GET['wpm'];
$d=isset($_GET['name']) && $_GET['name'];




  $age = mysql_real_escape_string($age);
  $sex = mysql_real_escape_string($sex);
  $wpm = mysql_real_escape_string($wpm);
  $name = mysql_real_escape_string($name);
   //build query
  $query = "INSERT INTO `ajax_example`(`ae_name`, `ae_age`, `ae_sex`, `ae_wpm`)
   VALUES ('$name','$age','$sex','$wpm')";
   $qry = mysql_query($query) or die(mysql_error());







$a="SELECT `ae_name`, `ae_age`, `ae_sex`, `ae_wpm` FROM `ajax_example`";
$qry_result=mysql_query($a) or die(mysql_error());





//Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Sex</th>";
$display_string .= "<th>WPM</th>";
$display_string .= "</tr>";


// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
$display_string .= "<tr>";
$display_string .= "<td>$row[ae_name]</td>";
$display_string .= "<td>$row[ae_age]</td>";
$display_string .= "<td>$row[ae_sex]</td>";
$display_string .= "<td>$row[ae_wpm]</td>";
$display_string .= "</tr>";

}


$display_string .= "</table>";
echo $display_string;
?>

 

 

if u could tell me show good websites to practice javascript. woulg b gr8.. thank u in advances.

Link to comment
Share on other sites

You have to add a field to the table, where you save a boolean that tells whether or not this is the first time the user has logged on. Set it to 0 (false) by default.

Then, when the user logs in, retrieve the flag with the rest of the details. If it's set to 0 then show the form, and update the flag to read 1 (true) at the same time as you save the other details from the form.

Edited by Christian F.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.