Jump to content

PHP and Javascript between them losing a plus sign


ocpaul20

Recommended Posts

OK, I have tried to make the code as short as possible.

 

The problem is this:

if I enter '3+' into the box on the screen and press click, I get back '3 ' (3+space)

This is obviously because of the urlencoding on the javascript call

but how to get it out with a '+' at the PHP end? I have tried urldecode()

and that does not work as I want.

Thanks for any help. I am sure it is a simple solution!

Paul

 

testscreen1.php

===========

<?php
// st_quest_editscreen.php
?>
<html>
<head>
<TITLE> test screen 1</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<SCRIPT type="text/javascript">
// ================ ajax stuff =====================

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function getValue() {
var val = document.form1.textname.value;
sndReqArg(val);
}

function sndReqArg(val) {
var params = 'val='+val;
var url = 'testprog1.php';
http.open("POST", 'testprog1.php', true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = handleResponse; // function to handle response
http.send(params);
  }

function handleResponse() {
if(http.readyState == 4 && http.status == 200) {
        var response = http.responseText;
	document.getElementById('qbox').innerHTML=response; // show text
	return(false);
}
}

</SCRIPT>
</head>
<body>
<div id="formbox" style="float:left;border:1px dashed black;">
		<form id="form1id" name="form1" method="post" action="#" >
				<div style="background-color:blue;color:white;height:20px;"  onClick="getValue();return(FALSE);">click</div>
				<input type="text" name="textname" title="text" value="" />
				<div id="qbox" style="border: 1px dotted black;">
				return value goes here
				</div> <!-- end of qbox -->
		</form>
</div> <!-- formbox -->
</body></html>

 

testprog1.php

========

<?php
// testprog1.php
$val = ($_POST['val']); // text
$uval = urldecode($val);
echo "This is testprog: val = [".$val."] urldecode(val)=[".$uval."]";exit;
?>

Try changing your sndReqArg js function to this.

 

function sndReqArg(val) {
val = encodeURIComponent(val);
var params = 'val='+val;
var url = 'testprog1.php';
http.open("POST", 'testprog1.php', true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = handleResponse; // function to handle response
http.send(params);
  }

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.