PHP Ajax Issue

I'm not what I am doing wrong here with my AJAX / PHP call.  Can you please have a look and let me know?  Thanks in advance.

On form Submit, call AJAX.
Ajax Performs PHP query and returns a single number.  Number is passed back to AJAX Script
Script places value into a form input field.
My PHP Page
<?php
$po = "1";	
$query_id = $this->EE->db->select('MAX(CAST(title AS UNSIGNED)) AS title', 'url_title')
                        ->where('LEFT(url_title, 6)=', 'order_')
                        ->where('channel_id', '29')
                        ->order_by('url_title', 'desc')
                        ->limit('1')
                        ->get('exp_channel_titles');     				
	
$order_id = $query_id->result_array();
		
$query_id->free_result();
		
	foreach ($order_id as $row)
	{
		$pn = $row['title'];
		$policy_number = $pn + $po;
	}	
		
	echo json_encode($policy_number);	

Open in new window

My AJAX Script;
$(document).ready(function() {
	var policyNumber{row_id} = function() {
		$.ajax({
			url: "index.php/ajax/policy_number.php",
			type: "POST",
			data: {
			},
				dataType: 'json',
				success: function(data) {
					policy_number = (data[0].title);
					$('input[name="item_options[{row_id}][policy_number]"]').val(policy_number);
				}
		});
	};
	$("form").on("submit", policyNumber{row_id});
});

Open in new window

LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
You don't have to access title value because your php script isn't returning it: it is in the query result, but you process the query result directly in your php script and you just return a number, or better a string containing a number. So the best thing you can do is to set the dataType in your Ajax call to text:

$(document).ready(function() {
	var policyNumber{row_id} = function() {
		$.ajax({
			url: "index.php/ajax/policy_number.php",
			type: "POST",
			data: {},
			dataType: 'text',
				success: function(policy_number) {
					$('input[name="item_options[{row_id}][policy_number]"]').val(policy_number);
				}
		});
	};
	$("form").on("submit", policyNumber{row_id});
});

Open in new window


and don't forget to change the last line of your php script this way

echo $policy_number;	

Open in new window

0
rgranlundAuthor Commented:
Nothing seems to happen when I click the submit button.  Here is my updated code:
<?php
$po = "1";	
$query_id = $this->EE->db->select('MAX(CAST(title AS UNSIGNED)) AS title', 'url_title')
                        ->where('LEFT(url_title, 6)=', 'order_')
                        ->where('channel_id', '29')
                        ->order_by('url_title', 'desc')
                        ->limit('1')
                        ->get('exp_channel_titles');     				
	
$order_id = $query_id->result_array();
		
$query_id->free_result();
		
	foreach ($order_id as $row)
	{
		$pn = $row['title'];
		$policy_number = $pn + $po;
	}	
		
	echo $policy_number;	

Open in new window

<script>
$(document).ready(function() {
	var policyNumber{row_id} = function() {
		$.ajax({
			url: "index.php/ajax/policy_number.php",
			type: "POST",
			data: {},
				dataType: 'text',
				success: function(policy_number) {
					$('input[name="item_options[{row_id}][policy_number]"]').val(policy_number);
				alert(policy_number);
				}
		});
	};
	$("form").on("submit", policyNumber{row_id});
});
</script>

Open in new window

0
Marco GasiFreelancerCommented:
Have you a live testing page when I can take a look using the console?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

rgranlundAuthor Commented:
What is happening, at the alert is the entire php script is being sent/alerted.
0
Marco GasiFreelancerCommented:
The last post for today (I'm going to sleep): are you using a framework? Perhaps CodeIgniter? I say this because I see the url of your script: "index.php/ajax/policy_number.php"
So, what I say now is valid in the assumption you're using CodeIgniter.
To make ajax calls work in CI you have to do two things:

1. In the footer of your page place this:

<script type="text/javascript">
	var baseurl = "<?php print base_url(); ?>";
</script>

Open in new window

This way the base_url will be available to all your javascript as baseurl.

2. change your ajax call url this way:
url: baseurl + "ajax/policy_number.php",

Open in new window


But the issue seems to be that your php code is not executed, as php be not installed or your server ignore it and I can't understand this at all...
0
rgranlundAuthor Commented:
I did that and it is still alerting back the entire php code.
0
Marco GasiFreelancerCommented:
I'm very very sorry, rgranlund, but yesterday I was too tired! If you use CI you can't directly call a script, you have to call a Controller method. Suppose your Controller is called Main, you have to create a method called policy_number and put here the code of your script

public function policy_number()
{
   $po = "1";	
   $query_id = $this->EE->db->select('MAX(CAST(title AS UNSIGNED)) AS title', 'url_title')
                        ->where('LEFT(url_title, 6)=', 'order_')
                        ->where('channel_id', '29')
                        ->order_by('url_title', 'desc')
                        ->limit('1')
                        ->get('exp_channel_titles');     				
   $order_id = $query_id->result_array();
   $query_id->free_result();
   foreach ($order_id as $row)
   {
	$pn = $row['title'];
	$policy_number = $pn + $po;
   }	
   echo $policy_number;	
}

Open in new window

Of course, the correct way would be to put database queries in a model method and to call that method, but that is up to you if you want to do it.
Then you Ajax call url should be:
   url: baseurl + "main/policy_number",

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Marco GasiFreelancerCommented:
But if you're using EspressionEngine, I'm not sure at all what I said could be useful for you. Have you implemented ome working Ajax call in this projector this one is the first?
0
rgranlundAuthor Commented:
Marco, I had a typo.  After I cleaned up the code and emulated what you did, everything was fine.
0
Marco GasiFreelancerCommented:
Fantastic! But can you tell me if you are using EE or CI? Because I use CodeIgniter and jut this morning, looking around for ifo to help you I realized that ExpresisonEngine is by ElliLab too but I don't understand if there is some structural link between them. What I read makes me thik they are substantially two different and not related products, but maybe you have some first-hand interesting info...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.