Dynamic SELECT fields with jQuery, Ajax, PHP and MySQL

Now we create the insert form. As we can see, in the RE_Prov select I have put an attribute that we will use to modify it. If it is empty when inserting, nothing happens.

echo “

Name
Province
Population

“;
?>

This is the Script that we will use to search the database.


Script details:

To know the value of the province: var number =document.getElementById(“province”).value;

When we move this select we write in search…. this is put here:

var to=document.getElementById(“Searching”);

to.innerHTML=”looking for….”;

We send the desired information to the document busqueda.php in post and send this data:

data: ‘idnumber=’+number+’&idpopula=”+population,

idnumber is the value of the province select and idpopula is the attribute but now it is empty.

When the search is done we will show it here:

jQuery(“#populationList’).html(a);

and we will delete it looking for… like this:

var to=document.getElementById(“Searching”);
to.innerHTML=””;

We put the insertion to the database.

if($_POST){
$name=addslashes($_POST);
$RE_Prov=addslashes($_POST);
$RE_popula=addslashes($_POST);

$insertar=mysql_query(“INSERT clients (name,province,population) VALUES (‘$name’,’$RE_Prov’,’$RE_popula’)”,$link);
if ($insert==1){echo ““;}else{echo ““;}
}
?>

and we close:



?>

Now we are going to explain what is in search.php:

First we put the connection and its search to the database and we show the results.

Remember that from insert.php and modify.php two variables are sent by POST which are $_POST which is the value of the province and then $_POST which is the value of the population.

function Connect()
{
if (!($link=mysql_connect(“localhost”,”root”,””)))
{
echo “Error connecting to database.”;
exit();
}
if (!mysql_select_db(“webdevelopment”,$link))
{
echo “Error selecting database.”;
exit();
}
return $link;
}
$link=Connect();

$B_BUSCAR= mysql_query (“SELECT * FROM populations where idprovincia=””.$_POST.”” order by population asc”,$link);
$R_FIND=mysql_fetch_assoc($B_FIND);
$C_LOOKUP=mysql_num_rows($B_LOOKUP);
if($C_LOOKUP){
do{
if($_POST==$R_SEARCH){$TRUE=” selected=’TRUE'”;}else{$TRUE=””;}
echo ““;

}while($R_FIND=mysql_fetch_assoc($B_FIND));
}else{
echo ““;
}
mysql_close($link);
?>
?>

Now we are going to create the modify form, but first we are going to create the list with the results that exist in the customer log and then we will go to the form through a link.

// To make it simple I have put a variable called ac and if it does not exist the list is shown, this variable is sent by GET.
if(!$_GET){
echo “

Listing

“;
$B_SEARCH= mysql_query (“SELECT * FROM clients”,$link);
$R_FIND=mysql_fetch_assoc($B_FIND);
$C_LOOKUP=mysql_num_rows($B_LOOKUP);
$sum=0;
do{ ++$sum;
echo “

“;
}while($R_FIND=mysql_fetch_assoc($B_FIND));
echo “

$sum “.stripslashes($R_SEARCH).” (Modify)

“;
}else{
// Now we search for the selected data.

$B_customer= mysql_query (“SELECT * FROM customers where id='”.$_GET.”‘”,$link);
$R_customer=mysql_fetch_assoc($B_customer);
$C_customer=mysql_num_rows($B_customer);

// We retrieve the information and put it into a variable.
$name=stripslashes($R_customer);
$RE_Prov=stripslashes($R_customer);
$RE_popula=stripslashes($R_customer);

threw out “

Modify

“;
// Now if we use the attribute poblacioattri where we put the variable of the current population inside
threw out “

Name
Province
Population

“;
}
?>
?>

We put the script where we do recover the poblacioattri attribute and the way to recover it is like this:

var population = jQuery(this).attr(“populationattri”);


?>

And we modify:

if($_POST){
$name=addslashes($_POST);
$RE_Prov=addslashes($_POST);
$RE_popula=addslashes($_POST);

$mod=mysql_query(“UPDATE clients SET name=”$name”,province=”$RE_Prov”,population=’$RE_popula’ WHERE id=”.$_POST.””,$link);
if ($mod==1){echo ““;}else{echo ““;}
}
?>

and we close:



See also  Show calendar PHP I
Loading Facebook Comments ...
Loading Disqus Comments ...