Forum Moderators: coopster

Message Too Old, No Replies

javascript vars to php

         

ploppy

6:52 pm on Aug 6, 2010 (gmt 0)

10+ Year Member



hello
i am using jquery with facybox to add and update records from a grid. what i would like to do is to call a js function which will gather the results and then use a function to pass to php so i can then use $_GET to include these values in an update form. This line in the script call conedit.php which then displays the form with the values already in the text boxes.

location.replace('./conedit.php?id='+itemlist);

instead of going to external page i would like to perhaps use a function to get these values and pass these values to a form using php_self option and gather the results of the js such as:

$id = $_GET['id'];
$val = $_GET['val'];
$itemlist = $_GET['itemlist'];
$items = rtrim($_GET['items'],",");

$result = mysql_query("SELECT * FROM `contact_con` WHERE `id` IN ($id)");


$row = mysql_fetch_array($result);

$id2 = $row['id'];
$name = $row ['name'];

hopefully you get the idea. my main concern at the moment is to find a way to het the values from js and pass them to php using $_GET. is there a way to that? here is the js.

<script type="text/javascript">
function edit(com, grid) {
if (com == 'Edit') {
if($('.trSelected').length>0){
if($('.trSelected').length>1){
alert('Please select just one register');
return;
}
var items = $('.trSelected');
var itemlist ='';
for(i=0;i<items.length;i++){
itemlist+= items[i].id.substr(3);
}
location.replace('./conedit.php?id='+itemlist);
return;
} else{
alert('Please select a row to edit.');
}
}
}
</script>


i wasn't sure wether to post here or the javascript forum. if this is incorrect forum please accept my apologies.

thanks

Matthew1980

7:09 pm on Aug 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Ploppy,

Relevant forum indeed! Well I guess it is seeing as the answer is a php answer :)

location.replace('./conedit.php?id='+itemlist);


would be (presuming as +itemlist is the get?):-

location.replace('./conedit.php?id=<?php echo $_GET['itemlist'];?>');


But if you are using $_GET (or $_POST for that matter) please sanitise the data before using it in conjunction with a sql query ;) Use mysql_real_escape_string() & strip_tags() around the $_GET, this will just make your data in the db that little bit more impervious.

Hopefully I have the correct end of the stick here ;-)

Cheers,
MRb

ploppy

8:12 pm on Aug 6, 2010 (gmt 0)

10+ Year Member



hello matthew

thanks for reply. what i am trying to do is do away with calling the location.replace area and find a way to capture the values from the javascript function? i cannot see a way to attach a file so i shall post whole code and perhaps that will give you a clearer picture.

<?php include("../../header.php"); ?>
<?php

$id = $_GET['id'];
$val = $_GET['val'];
$itemlist = $_GET['itemlist'];
$items = rtrim($_GET['items'],",");

$result = mysql_query("SELECT * FROM `contact_con` WHERE `id` IN ($id)");


$row = mysql_fetch_array($result);

$id2 = $row['id'];
$name = $row ['name'];
$email = $row ['email_con'];
$phone = $row ['phone_con'];
$fax = $row ['fax_con'];
$mobile = $row ['mobile_con'];
$notes = $row ['notes_con'];
?>
<script type="text/javascript">
$(function() {
$("#flex1").flexigrid(
{
url: 'staff.php',
dataType: 'json',
colModel : [
{display: 'ID', name : 'id', width : 40, sortable : true, hide:false, align: 'left'},
{display: 'Code', name : 'idcode_con', width : 75, sortable : true, align: 'left'},
{display: 'Company Code', name : 'idcom_con', width : 75, sortable : true, hide: true, align: 'left'},
{display: 'Name', name : 'name', width : 150, sortable : true, align: 'left'},
{display: 'Email', name : 'email_con', width : 150, sortable : true, align: 'left'},
{display: 'Phone', name : 'phone_con', width : 100, sortable : true, align: 'left'},
{display: 'Mobile', name : 'mobile_con', width : 100, sortable : true, align: 'left'},
{display: 'Fax', name : 'fax_con', width : 100, sortable : true, align: 'left'},
{display: 'Notes', name : 'notes_con', width : 230, sortable : true, align: 'left'}
],
buttons : [
{name: 'Add', bclass: 'add', onpress : openfacybox},
{separator: true},
{name: 'Edit', bclass: 'edit', onpress : edit},
{separator: true},
{name: 'Delete', bclass: 'delete', onpress : test},
{separator: true}
],
searchitems : [
{display: 'Name', name : 'name', isdefault: true},
{display: 'Email', name : 'email_con'},
{display: 'Code', name : 'idcode_con'}
],
sortname: "id",
sortorder: "asc",
usepager: true,
title: "Admin Contacts",
useRp: true,
rpOptions: [10,15,25],
rp: 10,
showTableToggleBtn: false,
resizable: false,
height: 'auto',
singleSelect: true,
nomsg: 'No Items'
}

);
});
function test(com,grid)
{
if (com=='Delete')
{
if($('.trSelected',grid).length>0){
if(confirm('Delete ' + $('.trSelected',grid).length + ' items?')){
var items = $('.trSelected',grid);
var itemlist ='';
for(i=0;i<items.length;i++){
itemlist+= items[i].id.substr(3)+",";
}
$.ajax({
type: "POST",
dataType: "json",
url: "delcontact.php",
data: "items="+itemlist,
success: function(data){
alert("Query: "+data.query+" - Total affected rows: "+data.total);
$("#flex1").flexReload();
}
});
}
} else {
alert('You have to select a row to delete.');
}
}


}

</script>
</div>
<script type="text/javascript">
function edit(com, grid) {
if (com == 'Edit') {
if($('.trSelected').length>0){
if($('.trSelected').length>1){
alert('Please select just one register');
return;
}
var items = $('.trSelected');
var itemlist ='';
for(i=0;i<items.length;i++){
itemlist+= items[i].id.substr(3);
}
location.replace('./conedit.php?id='+itemlist);
return;
} else{
alert('Please select a row to edit.');
}
}
}
</script>
<script type="text/javascript">
function openfacybox(){

jQuery.facybox({ div: '#form' });



}


</script>
<script type="text/javascript">
function conedit(){

jQuery.facybox({ div: '#editform' });



}


</script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('a[rel*=facybox]').facybox({
loading_image : 'loading.gif',
close_image : 'closelabel.gif'
})
})



</script>



<br />
<br />
<br />
<br />
<br />
<br />
<br />
<div id="container">
<table id="flex1" style="display:none;">
</table>

<br />
<br />
<div id="form" style="display:none;">
<form action="add1.php" method="post" class="webform">
<fieldset>
<legend><span>Enter New Contact</span></legend>
<label for="company">Company</label>
<select name="company">
<option SELECTED VALUE="">Select a code</option>
<option value="example1">example1</option>
<option value="example2">example2</option>
</select>
<label for="name">Full Name:</label>
<input id="name" name="name" class="text" type="text" />
<label for="email">Email address:</label>
<input id="email" name="email" class="text" type="text" />
<label for="phone">Telephone:</label>
<input id="phone" name="phone" class="text" type="text" />
<label for="mobile">Mobile:</label>
<input id="mobile" name="mobile" class="text" type="text" />
<label for="fax">Fax:</label>
<input id="fax" name="fax" class="text" type="text" />
<label for="notes">Notes:</label>
<textarea name="notes" cols="25" rows="3"></textarea>
</fieldset>
<input class="submit" type="submit" name="submit" value="Add Contact" />
</form></div>

<div id="editform" style="display:none;">
<form action="<?php echo $PHP_SELF;?>" method="post" class="webform">
<fieldset>
<legend><span>Update Contact</span></legend>
<br /> <div id="dataText">Please delete the fields if there is no data 'No data to display'.</div>

<label for="name">Full Name:</label>
<input id="name" name="name" class="text" type="text" value="<?php echo $name; ?>" />

<label for="email">Email address:</label>
<input id="email" name="email" class="text" type="text" value="<?php echo $email; ?>" />

<label for="phone">Telephone:</label>
<input id="phone" name="phone" class="text" type="text" value="<?php echo $phone; ?>" />

<label for="mobile">Mobile:</label>
<input id="mobile" name="mobile" class="text" type="text" value="<?php echo $mobile; ?>" />

<label for="fax">Fax:</label>
<input id="fax" name="fax" class="text" type="text" value="<?php echo $fax; ?>" />

<label for="notes">Notes:</label>
<textarea name="notes" cols="25" rows="3"><?php echo STRIPSLASHES(TRIM($notes)); ?></textarea>


</fieldset>
<input class="submit" type="submit" name="submit" value="Update" />
</form>
</div></div>
<br />
<br />
<br />
<?php include("../../footer.php"); ?>


instead of this line:location.replace('./conedit.php?id='+itemlist);

have something like:jQuery.facybox({ div: '#editform' });
and somehow include the values there? thanks

enigma1

7:16 pm on Aug 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the jquery form plugin

You can also have the form fields passed via ajax manually but I find it easier using the extension.

Finally once the processing is done on the server end just update the html with the result from the server end - the ajax response.