Page 2 of 2

Re: Javascript functions and php foreach values

Posted: Mon May 14, 2012 12:35 pm
by Celauran
Pavilion wrote:Is it possible to construct a listener and reference an input ID or input name?
Absolutely. I'm fairly certain you've already done so elsewhere.

Code: Select all

$('#element_id').change(function() {
    // blah blah blah
});

Re: Javascript functions and php foreach values

Posted: Mon May 14, 2012 12:45 pm
by Pavilion
Pavilion wrote:Is it possible to construct a listener and reference an input ID or input name?
Absolutely. I'm fairly certain you've already done so elsewhere.

Code: Select all

$('#element_id').change(function() {
    // blah blah blah
});
OK - but what is confusing me is this statement
Also, I noticed you still have id='chk_Bx' everywhere. This can lead to problems.
As a refresher, this checkbox is in a table. So... I can understand (if id's are suppose to be unique to a page) why (in a table situation id's shouldn't be used). But.... then how can I reference an element's id or name in a table situation? Or am I just misunderstanding your cautions about ID's and unique values within a page?

Thanks again - Pavilion

Re: Javascript functions and php foreach values

Posted: Mon May 14, 2012 12:54 pm
by Celauran
You reference IDs as specified above, whether they're part of a form, table, or otherwise. To access a form input by name, use something like

Code: Select all

$('input[name=whatever]').change(function() {
    // do stuff
});

Re: Javascript functions and php foreach values

Posted: Wed May 16, 2012 9:07 am
by Pavilion
Celauran wrote:You reference IDs as specified above, whether they're part of a form, table, or otherwise. To access a form input by name, use something like

Code: Select all

$('input[name=whatever]').change(function() {
    // do stuff
});
OK - same problem, new twist:

I just want to reference one button on my form, by name.

Following is the html for the button:

Code: Select all

<button type="button" class="button" name="send_csv">Send CSV</button>
This is my current attempt at writing a listener:

Code: Select all

$('input:button[name=send_csv]').click(function() {
alert("you found the listener");
});
This guide has been somewhat helpful. It is where I figured out the syntax input:button might help. But nothing seems to work. Whatever syntax I use, I am not triggering the alert.

Any assistance you can give is really appreciated.

Thanks Much: Pavilion

Re: Javascript functions and php foreach values

Posted: Wed May 16, 2012 11:57 am
by Pavilion
Well... I've figured out how to reference a button by name. The syntax is as follows:

Code: Select all

	$("button[name=send_csv]").click(function() {
		var pass_file = document.addressbk_frm.file.value;
		$('#demo').html(pass_file);
		
		$.post("addressbk_data_1.php",{bind_file:pass_file},function(data){
		$('#demo').html(data);
		});
		
	});
But now there is another problem. var pass_file = document.addressbk_frm.file.value; references <input type="file" style="height:20px" name="file"/>.

Currently - the uploaded file is being processed by submit to the same page. I am trying to move the processing of this file to a separate .php file, and am using the .click(function) to trigger the processing.

The following $.post is used to pass the user uploaded file to the processing .php page.

Code: Select all

$.post("addressbk_data_1.php",{bind_file:pass_file},function(data){
$('#demo').html(data);
});
However, the processing php file is not treating $.post("addressbk_data_1.php",{bind_file:pass_file},function(data) as a file.

My .php processing file is as follows:

Code: Select all

<?php
session_start();
// include database connection file, if connection doesn't work the include file will throw an error message
include '../tfm/include/db_connect.php';

// include data process file - contains custom functions.
include '../tfm/include/custom_functions.php';

// Define user_id and record date for INSERT and UPDATE Statements
$userid = $_SESSION['user_id'];
$firstcontact = date("Y-m-d");

/////////-------------testing here
$file = $_POST['bind_file'];
$_FILES["file"]["tmp_name"] = $_POST['bind_file'];
if (file_exists($file));
{
echo "files variable: " . $_FILES['file']['tmp_name'] . "<br />";

echo "file type: " . $_FILES['file']['type'] . "<br />";
}

///////////----------------End testing

?>
This echo line returns a value:

Code: Select all

echo "files variable: " . $_FILES['file']['tmp_name'] . "<br />";
This echo line does not return a value:

Code: Select all

echo "file type: " . $_FILES['file']['type'] . "<br />";
__________________________________________________

Since the file is currently being processed through a submit to the same page, the $_FILES variable just picks up the file from the $_POST. But it seems passing user input through a jQuery $.post changes the input and my processing page does not recognize it as a file. How do I remedy this?

Thanks so much - Pavilion

Re: Javascript functions and php foreach values

Posted: Wed May 16, 2012 1:28 pm
by Celauran

Re: Javascript functions and php foreach values

Posted: Wed May 16, 2012 9:34 pm
by Pavilion
Thanks Celauran - but after reviewing the link you provided, I've decided to hold off. My current php page is working, I thought I could clean it up a bit. But, there are more important things for me to learn right now. Your help is truly appreciated though. You no idea how much I've learned.

Pavilion

Re: Javascript functions and php foreach values

Posted: Fri May 18, 2012 8:31 pm
by Pavilion
New question on javascript (well - at least I think it's related to javascript) :? :?

The php page I'm working on now has:
  1. A form with the post method. The form posts to itself.
  2. Several jQuery routines
  3. A new javascript routine - which runs a search on my data table.
Here is my problem. Whenever I hit the ENTER key on this page - the following error message appears:
File not found. Make sure you specified the correct path.
Here's the kicker - the form posts to the same page, and the url is the address of my php page.

I'm sensing there is some kind of conflict with my javascript routines. This is because the most recent javascript routine includes the following:

Code: Select all

document.onkeypress=KeyPress;
function KeyPress(e)
{
var code =(window.event)? event.keyCode : e.which;
if(code==13) {alert('Enter key');}
}
This function works in Internet explorer. But in Firefox - the alert doesn't popup and I get the above message.

Every single input on my form acts the same way if I press the ENTER key. In Firefox - I'm given the error message. In IE ENTER acts as it should.

All help with this puzzle would be deeply appreciated.

Thanks Much:

Pavilion

_________________________________

Edit - there is a conflict between the ENTER key behavior and the fact that the form is posting to itself. The message I recieve after hitting the ENTER key...
File not found. Make sure you specified the correct path.
Is actually a message generated if the POST routines cannot find a CSV file uploaded by users. So... now that I know where the conflict is (and it is NOT acting this way in IE) - how do I get Firefox to stop "seeing" the ENTER key as a submit?

Thanks again - Pavilon

Re: Javascript functions and php foreach values

Posted: Sat May 19, 2012 9:15 am
by Celauran

Code: Select all

$(whatever).keypress(function(e) {
    if (e.which == 13) {
        e.preventDefault();
    }
});

Re: Javascript functions and php foreach values

Posted: Sat May 19, 2012 11:24 am
by Pavilion
Celauran wrote:

Code: Select all

$(whatever).keypress(function(e) {
    if (e.which == 13) {
        e.preventDefault();
    }
});
Thanks - Celauran
An edit to my question - I was going to ask how to use this globally (on a page). But, then I realized I need to grab hold of the ENTER key behavior on the search function, so now my plan is to attach event functions to all of my other input. An example would be as follows:

Code: Select all

<input class="a" tabindex="1" type="text" id='f_name' name='fname' onkeypress="prevent_default();"/>
With my javascript function as follows:

Code: Select all

	prevent_default=function(e) 
	{
		// alert("found it"); - this alert works
               alert(e.which); // this alert does not pop up (what am I doing wrong)?
		if (e.which == 13) {
                e.preventDefault();
         }
	};
Thanks in Advance:

Pavilion

Re: Javascript functions and php foreach values

Posted: Sun May 27, 2012 8:40 pm
by Pavilion
OK - I've figured out to write a function that does the following:
  1. Prevents default ENTER key behavior on selected input elements. The function is called from the element event as follows:

    Code: Select all

    <input tabindex="1" type="text" id='f_name' name='fname' onkeypress='return prevent_default(this,event)'/>
  2. Changes ENTER keystroke into TAB key behavior
The prevent_default function follows

Code: Select all

	function prevent_default(obj,e) {
	var isfocus=0
	if (isfocus==1) return
	var key_stroke=(window.event)? event.keyCode : e.which;
	var var_next = $(this).next(':input').name;
	console.log("your key stroke is: ") + console.log(key_stroke);
		if (key_stroke == 13) {
		 var ele = document.forms[0].elements; 
			for(var i=0;i<ele.length;i++){ 
			var q=(i==ele.length-1)?0:i+1;// if last element : if any other 
			if(obj==ele[i]){ele[q].focus();break} 
		}
		e.preventDefault();
		}
	}
Here is my question. It is definitely possible that I'll want to use this same function over and over again on different pages. I've a custom_functions.js file that holds other custom functions that will be used repeatedly. But, I can't figure out how to rework this function so that it will operate from an external file. Every thing I try does NOT work.

Does anyone here have any suggestions on how I can rework the above function to work from an external file?


Thank you in advance:

Pavilion