Jump to content

Read Javascript in PHP


acsonline

Recommended Posts

Hey Guys,

 

I have a JS script for booking time slots for a delivery.

 

On the same page I have the payment system etc, so I'm just wondering how would i read the selected options from the JS and input it into the order details on in the PHP system.

 

The booking system is loaded by calling

 

<script language="javascript" src="/booking/load.php?view=2&cid=1"></script>

 

Contents of Load.php

<?php
error_reporting(0);
include("options.php");
if (!isset($_REQUEST["cid"])) { $calendarId = '1'; } else { $calendarId = $_REQUEST["cid"]; };
if (!isset($_REQUEST["view"])) { $view = '1'; } else { $view = $_REQUEST["view"]; };

$sql = "SELECT * FROM ".$TABLES["Calendars"]." WHERE id='".$_REQUEST["cid"]."'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
$Calendar = mysql_fetch_assoc($sql_result);
$CalendarOptions = unserialize($Calendar["options"]);


$loadCalendar = '
<div id="AvailabilityCalendar'.$_REQUEST["cid"].'"></div>
<div id="DateBookings'.$_REQUEST["cid"].'" style="margin:0px; padding:0px"></div>

<script language="javascript">
ajaxpage("'.$SETTINGS["installFolder"].'load-calendar.php?view='.$view.'&cid='.$calendarId.'","AvailabilityCalendar'.$_REQUEST["cid"].'","get");
</script>
';

$loadCalendar = preg_replace("/\n/","",$loadCalendar);
$loadCalendar = preg_replace("/\r/","",$loadCalendar);


?>
  
var flagCaptcha = false;
var flagFields = true;
var maxSlots=<?php echo $CalendarOptions["bookingLenght"]; ?>;
var message = 'Please fill in all mandatory fields ! \n';

var bustcachevar=1; //bust potential caching of external pages after initial request? (1=yes, 0=no)
var bustcacheparameter="";

function checkForm() {
all_inputs=document.getElementsByTagName("input");
for (i=0;i<all_inputs.length;i++) {
	if (all_inputs[i].type=="checkbox") {
		if ((all_inputs[i].checked)&&(! all_inputs[i].disabled)) {
			return true;
		}
	}
}
    alert("Select at least 1 time slot");
    return false;
}

function selectTimeSlot() {
disableElements();
    singlePrice=document.getElementById("singlePrice").value;
    price=0;
all_inputs=document.getElementsByTagName("input");
for (i=0;i<all_inputs.length;i++) {
	if (all_inputs[i].type=="checkbox") {
		if ((all_inputs[i].checked)&&(! all_inputs[i].disabled)) {
			price=parseFloat(price)+parseFloat(singlePrice);
		}
	}
}
    document.getElementById("price").value=price;
    document.getElementById("priceDiv").innerHTML=price;
    
}

function disableElements() {
first=-1;
    last=-1;
    emptySpot=false;
    all_inputs=document.getElementsByTagName("input");
for (i=0;i<all_inputs.length;i++) {
	if (all_inputs[i].type=="checkbox") {
		if ((all_inputs[i].checked)&&(! all_inputs[i].disabled)) {
			if (emptySpot) {
                	all_inputs[i].checked=false;
                } else {
                    if (first<0) {
                        first=i;
                        last=i;
                    }
                    if ((i-last)>1) {
                        all_inputs[i].checked=false;
                        emptySpot=true;
                    } else {
                        last=i;
                    }
                }
		}
	}
}
    if ((last-first+1)==maxSlots) {
    	reachedMax=1;
    } else {
    	reachedMax=0;
    }
    if (first>=0 && last>=0) {
        for (i=0;i<all_inputs.length;i++) {
            if (all_inputs[i].type=="checkbox") {
                if ((i<(first-(1-reachedMax))) || (i>(last+(1-reachedMax)))) {
                    if ((! all_inputs[i].checked) && (! all_inputs[i].disabled)) {
                        all_inputs[i].disabled=true;
                    }
                } else {
                    if ((! all_inputs[i].checked)) {
                        all_inputs[i].disabled=false;
                    }
                }
            }
        }
     } else {
     	for (i=0;i<all_inputs.length;i++) {
            if (all_inputs[i].type=="checkbox") {
                    if ((! all_inputs[i].checked)) {
                        all_inputs[i].disabled=false;
                    }
            }
        }
     }
}

function createRequestObject(){
try	{
	xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
}	catch(e)	{
	alert('Sorry, but your browser doesn\'t support XMLHttpRequest.');
};
return xmlhttp;
};

function ajaxpage(url, containerid, requesttype){
var page_request = createRequestObject();
if (requesttype=='get'){
	if (bustcachevar) bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
	page_request.open('GET', url+bustcacheparameter, true)
	page_request.send(null)
} else if (requesttype=='post') {
	page_request.open('POST', url, true);
	page_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	page_request.setRequestHeader("Content-length", poststr.length);
	page_request.setRequestHeader("Connection", "close");
	page_request.send(poststr);
};

page_request.onreadystatechange=function(){
	loadpage(page_request, containerid)
}

}

function ShowToolTip(object) {
    document.getElementById(object).style.visibility = 'visible';
}

function HideToolTip(object) {
    document.getElementById(object).style.visibility = 'hidden';
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
    document.getElementById(containerid).innerHTML=page_request.responseText;
};
}

function submitDate(date) {
ajaxpage("<?php echo $SETTINGS["installFolder"];?>load-calendar.php?perDay="+date+"&view=<?php echo $view; ?>&cid=<?php echo $calendarId; ?>","AvailabilityCalendar<?php echo $_REQUEST["cid"]; ?>","get");
}

function submitBooking(startdate,sMonth,sYear) {
var poststr = "ac=book&startDate="+startdate+"&customerName=" + encodeURIComponent( document.frm.customerName.value ) + "&email=" + encodeURIComponent( document.frm.email.value ) + "&phone=" + encodeURIComponent( document.frm.phone.value ) + "&notes=" + encodeURIComponent( document.frm.notes.value ) + "&price=" + encodeURIComponent( document.frm.price.value );
    alert(poststr);
    all_inputs=document.getElementsByTagName("input");
for (i=0;i<all_inputs.length;i++) {
	if (all_inputs[i].type=="checkbox") {
		if ((all_inputs[i].checked)&&(! all_inputs[i].disabled)) {
			poststr=poststr+"&"+parseInt(all_inputs[i].id)+"=on";
		}
	}
}
ajaxpage('<?php echo $SETTINGS["installFolder"]; ?>load-bookingform.php?ac=book&cid=<?php echo $_REQUEST["cid"]; ?>&'+poststr,'DateBookings<?php echo $_REQUEST["cid"]; ?>','get');
var start = new Date().getTime();
    while (new Date().getTime() < start + 500);
ajaxpage('<?php echo $SETTINGS["installFolder"].'load-calendar.php?view='.$view; ?>&cid=<?php echo $_REQUEST["cid"]; ?>&month='+sMonth+'&year='+sYear,'AvailabilityCalendar<?php echo $_REQUEST["cid"]; ?>','get');
};


loadCalendar = '<?php echo $loadCalendar; ?>';
document.writeln(loadCalendar);

 

Any ideas?

 

 

Link to comment
Share on other sites

loading up a php file through a script tag, is likely your first problem. Although doable, the browser is still likely to render it as php and not javascript. Try adding

header("Content-type: application/x-javascript");

 

to the very top of the script you have shown. So the browser make think its javascript upon rendering while severside the php should still run properly.

 

Link to comment
Share on other sites

You can pass it as JSON or an Array or you can Put it in a hidden element for the javascript to read from say a hidden text field, then you use the javascript to take the value of that element then pass it through your java as you want. Its quick and dirty but it could work

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.