Page 1 of 1

Simple Javascript routine - and I still can't get it

Posted: Sun May 06, 2012 7:32 am
by Pavilion
I thought this would be fairly straight-forward, but something is off.

I've got a page functioning in which users can upload an email address file and insert contents into the database. The returned data displays very nicely into a table block. Now, I want to give users the ability to simply add additional contacts to their "address book" one at a time. I thought it would be a fairly straight-forward task of building inputs and using a submit input to trigger a javascript function. But, I can't even get the submit input to pick up the javascript. Following is my applicable script.

input - type submit

Code: Select all

<input type="submit" id ='submit_address' name='new_addrss'value='Submit New Address'>
Javascript

Code: Select all

<script>
/* Define variables. These variables will passed in an array for INSERT into time tables. */
var l_name = trim($_POST['lname']);
var f_name = trim($_POST['fname']);
var e_mail = trim($_POST['email']);
var dphone = trim($_POST['d_phone']);
var p_ext = trim($_POST['ext']);
var cphone = trim($_POST['cell']);
var j_title = trim($_POST['title']);
var record_date = "<?=$firstcontact?>";
var assign_to = "<?=$userid?>";

// document.ready function will not allow any functions to execute unless the page is fully loaded.
$(document).ready(function() {
	$(function () {

	pass_array = [];
	pass_array.push(l_name, f_name, e_mail, dphone, p_ext, cphone, j_title, record_date, assign_to);


	$('#submit_address').click(function() {

	$.post('test.php', {bind_array:pass_array}, function(data) {
	alert(data);
	});
	});
});
});

</script>
Here is the problem - when I click the submit button the following message appears:
File not found. Make sure you specified the correct path.
Needed information is as follows:
  1. The file test.php does exist and the path is correct
  2. I get this error message even if I remove EVERY LINE of javascript.
    It is acting as though the submit click never even triggers the Javascript.
Is there something wrong with my syntax:

Code: Select all

$('#submit_address').click(function()
that the javascript won't trigger when I click the following?

Code: Select all

<input type="submit" id ='submit_address' name='new_addrss'value='Submit New Address'>
Thanks Much:

Pavilion

Re: Simple Javascript routine - and I still can't get it

Posted: Mon May 07, 2012 12:42 pm
by x_mutatis_mutandis_x
You shouldn't be accessing your post values using $_POST in javascript. That's a PHP global variable.
Use something like document.getElementById('submit_address').value or document.form[0].new_addrss.value;

Re: Simple Javascript routine - and I still can't get it

Posted: Mon May 07, 2012 2:55 pm
by Pavilion
x_mutatis_mutandis_x wrote:You shouldn't be accessing your post values using $_POST in javascript. That's a PHP global variable.
Use something like document.getElementById('submit_address').value or document.form[0].new_addrss.value;
Hello "x_mutatis":

Yes - I was just starting to figure this out as you posted. But... now I've more questions. Following is my script (as it stands now):

Code: Select all

<script type="text/javascript">
var assignto = "<?=$userid?>";
var record_date = "<?=$firstcontact?>";

function proccessSingleAddress()
{
var l_name = document.addressbk_frm.lname.value;
var f_name = document.addressbk_frm.fname.value;
var e_mail = document.addressbk_frm.email.value;
var dphone = document.addressbk_frm.d_phone.value;
var extension = document.addressbk_frm.ext.value;
var cphone = document.addressbk_frm.cell.value;
var jtitle = document.addressbk_frm.title.value;

pass_array = [];
pass_array.push(l_name, f_name, e_mail, dphone, extension, cphone, jtitle, assignto, record_date);

document.getElementById("demo").innerHTML=pass_array;

	$.post("addressbk_data.php",{bind_array:pass_array},function(data){
		$('#addressbk').html(data);
	});
}
</script>
The javascript is functioning, now. It is picking up my post data and pushing it into the pass_array[]. Also the pass_array is being returned in my "demo" <p> (formatted appropriately).

At this point my question are more about process than syntax. As you may recall, previously I set up a routine so that users can upload a csv file and INSERT the contents into the database tables. You helped me figure out how to do this. The upload and INSERT routines are running on the same page as this javascript. In essence the same page:
  1. Allows users to upload csv email address files (this capability is functioning very nicely)
  2. Gives users the ability to add one email address at a time - I am working on this functionality right now.
  3. Will allow users to edit email address files in their "address book".
So... here is my question about process.

The queries used for INSERTing csv files and manually entered email address files are the same.
I will need to run the same clean up routines on phone numbers and email addresses.
I will need to trim all the same fields.

So... is it possible for me to gather the data in this favascript routine and then send the data through the same queries I've already built?

Or... does javascript require me to send the assembled array to a new php page and rebuild all the clean up routines and queries on a second page?


The following portion of my javascript is currently NOT active. It is inserted as a "next step".

Code: Select all

	$.post("addressbk_data.php",{bind_array:pass_array},function(data){
		$('#addressbk').html(data);
	});
If reality forces me to send the assembled array to a new addressbk_data.php file, then how do I send it back to the very same table addressbk currently used to display uploaoded email files? The current table feeds off a SELECT query on my existing addressbk.php page.

Thank you so much for your advice and help. It is appreciated.

Pavilion

Re: Simple Javascript routine - and I still can't get it

Posted: Tue May 08, 2012 7:25 am
by Pavilion
Hello Folks:

I'm bumping this thread up, hoping for some help on kosher javascript data processing. Specifically the following questions from my previous post:
So... is it possible for me to gather the data in this javascript routine and then send the data through the same queries I've already built?

Or... does javascript require me to send the assembled array to a new php page and rebuild all the clean up routines and queries on a second page?
Advice is honestly appreciated.

Thanks in advance:

Pavilion