javascript - Post values to database using MySQL, PHP, AJAX without using jQuery -
here page generates product display:
<?php $q = intval($_get['q']); $databaseconnect = $_server['document_root'] . "/637415/globalscripts/sql_connect.php"; include($databaseconnect); mysqli_select_db($con,"up637415_cms"); $sql="select * products prod_id = '".$q."'"; $result = mysqli_query($con,$sql); while($row = mysqli_fetch_array($result)) { echo '<div class="prodwrapper">' . '<h2>' . '<span id="currentprodid">' . $row['prod_id'] . '</span>' . ' ' . $row['prod_title'] . '</h2>' . '<form><input type="text" placeholder="enter new title" id="newprodtitle"/><input type="button" value="commit" onclick="producttitleupdate()"/></form> ' . '<div class="prodimg">' . '<img src="/637415/cms/images/products/' . $row['prod_img'] . '"' . ' ' . 'alt="' . $row['prod_title'] . ' ' . 'image' . '">' . '<form class="clearit"><input type="file" value="select image" /><input type="submit" value="upload & change image" /></form> ' . '</div>' . '<h3>' . 'product description:' . '</h3>' .$row['prod_description'] . '<form class="clearit"><textarea class="clearit" rows="4" cols="50" placeholder="update product description"></textarea><input type="submit" value="commit" /></form> ' . '<p>' . 'quantity available:' . $row['prod_quantity'] . '</p>' . '<form><input type="text" placeholder="enter quantity" /><input type="submit" value="commit" /></form> ' . '</div>' ; } mysqli_close($con); ?>
here javascript file i'm trying use post values span id="currentprodid" , input id of "newprodtitle" mysql database using php:
function producttitleupdate() { if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getelementbyid("newprodtitle").innerhtml=xmlhttp.responsetext; } } xmlhttp.open("post","/637415/admin/scripts/updateproducttitle.php",true); xmlhttp.send(newprodtitle=document.getelementbyid("newprodtitle").value); xmlhttp.send(newprodtitle=document.getelementbyid("currentprodid").value); }
here php file ajax uses:
<?php $databaseconnect = $_server['document_root'] . "/637415/globalscripts/sql_connect.php"; include($databaseconnect); if (!$con) { die('could not connect: ' . mysql_error()); } mysqli_select_db($con,"up637415_cms"); $sql="update products set prod_title='(newprodtitle javascript file) prod_id=(currentprodid javascript file)"; if (!mysql_query($sql,$con)) { die('error: ' . mysql_error()); } echo '1 record added' . ' ' . '<a href="/637415/admin/index.php">go admin</a>'; mysql_close($con) ?>
i not sure how value javascript file:
set prod_title='(newprodtitle javascript file) prod_id=(currentprodid javascript file)";
then replace values stored in database. should refresh new value without refreshing page. have been scanning on internet on how feeling bit lost.
any appreciated. thanks.
ok, using post
method in ajax request, pass javascript values php, , subsequently mysql, need send variable values php script looks doing.
typically, have serialize data trying send, so:
var data = 'newprodtitle='+newprodtitle+'¤tprodid='+currentprodid;
so ends looking when sent on network:
newprodtitle=blahbalh¤tprodid=8392
you have send data via xmlhttprequest on php. there's example here on how it.
finally, in php reference values receive $_post['newprodtitle']
, $_post['currentprodid']
Comments
Post a Comment