PHP - AJAX - Changing a form dynamically

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 4368 Views - Last Post: 09 June 2010 - 01:08 PM

#1 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 06:19 AM

Hey guys.

Instead of implenting a prefabricated calender on my site, I tried a different route. I built a simple form where the user picks time and date.

Now, what I need to do is to secure a dynamic changing of the <select> tag carrying the values for days in the month, based on what information the user has put into the <select> tag carrying the values for the month. This should be logical, as it should not be possible to choose ie. the 31st of June, as there's no such date. I hope you understand what I need.

So let's dive into what code I have so far.

My PHP looks like this for the $_GET:

if($_GET['input_text'])
{
 $month_pick = $_GET['input_text'];
 echo $month_pick;
 return;
}



My for-loop running the <option> tags within my <select> tag for the date of the month looks like this:

for($i = 0; $i < date('d', mktime(0, 0, 0, $month_pick, 0, $year)); $i++)



The variable "$year" is just a simple date function choosing this year.

My AJAX looks like this:

function test()
{
 var input_text = document.getElementById("input_text").value;
 var url = "calendar.php?input_text=" + escape(input_text);
		
 xmlHttp.open("GET", url, true);
 xmlHttp.onreadystatechange = update;
 xmlHttp.send();
}



The function update is for test usage, where I have a second field outputting the echoed value, so I can keep check on how the transaction is going.

Any suggestions on how I can crack this issue? My problem seems to be that the "$month_pick" variable doesn't change dynamically within the for-loop, therefore not restricting the amount of loops with the new month chosen. The test field I set up with the update function is working though, I am getting the name of the month I pick from the drop-down menu in the month <select> tag.

The month tag looks like this:

<select name="input_text" onchange="test();" id="input_text">



Is This A Good Question/Topic? 0
  • +

Replies To: PHP - AJAX - Changing a form dynamically

#2 webpeater  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 95
  • Joined: 04-June 10

Re: PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 06:33 AM

When you change the month selection you post/get that to a php script.

In the php script i suggest you use the function cal_days_in_month
Then you use a for loop to ouput the days in that month.

Which you use in your first file where you update the days in the selected month.


I think you know what you must do, reading your explanation ;)
I think the cal days function is the function you needed.

This post has been edited by webpeater: 09 June 2010 - 06:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

Re: PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 06:47 AM

View Postwebpeater, on 09 June 2010 - 05:33 AM, said:

When you change the month selection you post/get that to a php script.

In the php script i suggest you use the function cal_days_in_month
Then you use a for loop to ouput the days in that month.

Which you use in your first file where you update the days in the selected month.


I think you know what you must do, reading your explanation ;)
I think the cal days function is the function you needed.


That's what I figured as well, but it doesn't seem to work. I'm getting a warning saying: "<b>Warning</b>: cal_days_in_month() expects parameter 2 to be long, string given"

And the code looks like this:

$month_pick = $_GET['input_text'];
$cal = cal_days_in_month(0, $month_pick, $year)



But this messes up both what gets echoed out, and also the value being spit out in the options tag.
Was This Post Helpful? 0
  • +
  • -

#4 webpeater  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 95
  • Joined: 04-June 10

Re: PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 06:52 AM

You need to give the month as integer. So not 'June' but 6.
If you are trying to give an integer, you should try one of the two beneath
intval($month)
(int) $month


EDIT: In your case
$cal = cal_days_in_month(0, intval($_GET['input_text']), $year);

And the value of the options (in your html form) needs to be an integer !!

This post has been edited by webpeater: 09 June 2010 - 06:56 AM

Was This Post Helpful? 1
  • +
  • -

#5 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

Re: PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 07:12 AM

View Postwebpeater, on 09 June 2010 - 05:52 AM, said:

You need to give the month as integer. So not 'June' but 6.
If you are trying to give an integer, you should try one of the two beneath
intval($month)
(int) $month


EDIT: In your case
$cal = cal_days_in_month(0, intval($_GET['input_text']), $year);

And the value of the options (in your html form) needs to be an integer !!


Yep, that got rid of the warning, so now I know the function is running at least. And my test-field is outputting 02 for Feb, 03 for March and so on, so that's working too. I tried changing it to the $cal variable as well, just to check, and that's outputting 28 for Feb, 30 for June, 31 for July and such. So again, that's all working.

But the for-loop is still broken. With this setup it's returning blank values for the drop-down menu:

for($i = 0; $i < $cal; $i++)


I tried with a different setup too, but this just provides a static list of the 30 days in the current month - not the month picked in the other options tag - so that's 30 for this month of June. I don't understand why though, as $month_pick should be changing dynamically as it is in my test-field.

for($i = 0; $i < date('d', mktime(0, 0, 0, $month_pick, 0, $year)); $i++)


I know I'm missing something here, but I'm not good with math, so I can't quite see where I'm skipping a corner in the code.
Was This Post Helpful? 0
  • +
  • -

#6 webpeater  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 95
  • Joined: 04-June 10

Re: PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 07:29 AM

<?php
$month = 7;
for ($i = 1; $i<=cal_days_in_month(0, $month, date('Y')); $i++) {
  echo $i . ' ' . PHP_EOL;
}
?>



This code gives all days in the $month.
I'm no AJAX expert, but i think with this you can populate the options of a selection field.
Was This Post Helpful? 0
  • +
  • -

#7 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

Re: PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 07:39 AM

View Postwebpeater, on 09 June 2010 - 06:29 AM, said:

<?php
$month = 7;
for ($i = 1; $i<=cal_days_in_month(0, $month, date('Y')); $i++) {
  echo $i . ' ' . PHP_EOL;
}
?>



This code gives all days in the $month.
I'm no AJAX expert, but i think with this you can populate the options of a selection field.


Your code is similar to mine. I got this:

$month_pick = $_GET['input_text'];
$cal = cal_days_in_month(0, $month_pick, $year);
//and then for the loop:
for($i = 0; $i < $cal; $i++)



So I reckon it must be the AJAX code that's messing me up. Like I said, the echoed value works fine, but it doesnt seem to be inputting the dynamic variable into the for-loop for some reason.
Was This Post Helpful? 0
  • +
  • -

#8 webpeater  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 95
  • Joined: 04-June 10

Re: PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 07:59 AM

I suggest you take your question to the javascript forum.
Was This Post Helpful? 0
  • +
  • -

#9 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

Re: PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 08:22 AM

Hey guys. This is a double post as I was recommended on the PHP board to take this to the JS board as well. The original thread and the input given there is here: PHP - AJAX

Instead of implenting a prefabricated calender on my site, I tried a different route. I built a simple form where the user picks time and date.

Now, what I need to do is to secure a dynamic changing of the <select> tag carrying the values for days in the month, based on what information the user has put into the <select> tag carrying the values for the month. This should be logical, as it should not be possible to choose ie. the 31st of June, as there's no such date. I hope you understand what I need.

So let's dive into what code I have so far.

My PHP looks like this for the $_GET:

if($_GET['input_text'])
{
 $month_pick = $_GET['input_text'];
 $cal = cal_days_in_month(0, $month_pick, $year);
 echo $month_pick;
 return;
}



My for-loop running the <option> tags within my <select> tag for the date of the month looks like this:

for($i = 0; $i < $cal; $i++)



My AJAX looks like this:

function test()
{
 var input_text = document.getElementById("input_text").value;
 var url = "calendar.php?input_text=" + escape(input_text);
		
 xmlHttp.open("GET", url, true);
 xmlHttp.onreadystatechange = update;
 xmlHttp.send();
}



The function update is for test usage, where I have a second field outputting the echoed value, so I can keep check on how the transaction is going.

Any suggestions on how I can crack this issue? My problem seems to be that the "$month_pick" variable doesn't change dynamically within the for-loop, therefore not restricting the amount of loops with the new month chosen. The test field I set up with the update function is working though, I am getting the name of the month I pick from the drop-down menu in the month <select> tag.

The month tag looks like this:

<select name="input_text" onchange="test();" id="input_text">


Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,172
  • Joined: 08-June 10

Re: PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 08:32 AM

if I understand it right, you have a (fixed) <select> for the months and a (dynamic) <select> for the days? then you want to populate the second <select> using AJAX?

that is, the second <select> should be empty by default. to populate it there are 2 ways: populate by Javascript via DOM or inserting prepared code from PHP.

way #1 only requires you to receive the number of days, which has to go into a Javascript loop creating the <option> elements.
way #2 requires PHP to create the all <option>s based on the passed month, and Javascript only has to insert that chunk in your HTML.
Was This Post Helpful? 0
  • +
  • -

#11 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 08:33 AM

Topics merged, please don't post the same questions multiple times (whether in the same forum or a different forum)
Was This Post Helpful? 0
  • +
  • -

#12 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

Re: PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 08:57 AM

View PostDormilich, on 09 June 2010 - 07:32 AM, said:

if I understand it right, you have a (fixed) <select> for the months and a (dynamic) <select> for the days? then you want to populate the second <select> using AJAX?

that is, the second <select> should be empty by default. to populate it there are 2 ways: populate by Javascript via DOM or inserting prepared code from PHP.

way #1 only requires you to receive the number of days, which has to go into a Javascript loop creating the <option> elements.
way #2 requires PHP to create the all <option>s based on the passed month, and Javascript only has to insert that chunk in your HTML.


Way #1 sounds easy.

How to go about that? :)
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,172
  • Joined: 08-June 10

Re: PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 09:21 AM

depends if IE behaves *gg*

well, both ways have their pros and cons. and as usual, there are many ways to reach a goal.

so what you need in your update() function is basically a loop that creates <option>s based on the number given through .responseText. what does your current update function look like?
Was This Post Helpful? 0
  • +
  • -

#14 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

Re: PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 09:37 AM

View PostDormilich, on 09 June 2010 - 08:21 AM, said:

depends if IE behaves *gg*

well, both ways have their pros and cons. and as usual, there are many ways to reach a goal.

so what you need in your update() function is basically a loop that creates <option>s based on the number given through .responseText. what does your current update function look like?


It's purely for test purposes at the moment. Displaying the number of the month picked in a field. It looks like this:

function update()
{
 if(xmlHttp.readyState == 4)
 {
  var response = xmlHttp.responseText;
  document.getElementById("output_text").value = response;
 }
}



If I don't shape it out in a PHP loop, I wouldn't know how to go about creating the options tag so it's looped properly. But maybe you have suggestions for this?
Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,172
  • Joined: 08-June 10

Re: PHP - AJAX - Changing a form dynamically

Posted 09 June 2010 - 10:13 AM

creating an option tag in JS is simple:
var opt = document.createElement("option");

all you have to do is making that in a loop and attach it to the document tree.

This post has been edited by Dormilich: 09 June 2010 - 10:14 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2