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;
?>

Link to comment
Share on other sites

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);
  }

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.