Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by Michel Plungjan
at 2024-11-07 05:12:59
Point:500 Replies:15 POST_ID:828780USER_ID:11205
Topic:
PHP Scripting Language;;
http://plungjan.name/SO/load.php
The following code will fail if I do not trim the data.
Why does the PHP return "1 " or "0 " instead of "1" or "0" ?
The following code will fail if I do not trim the data.
Why does the PHP return "1 " or "0 " instead of "1" or "0" ?
<?php //load.phpif(isset($_POST["id"])){ $myid = $_POST['id']; if($myid == "1"){ echo "1"; }else if($myid == "0"){ echo "0"; }}else {?><!DOCTYPE html><html><head><title>Post something</title><script src="http://code.jquery.com/jquery-1.10.0.min.js"></script><script type="text/javascript">$(function() { $('#submit').click( function(){ $.post('load.php', {"id":$('#id').val()}, function(data){ window.console&&console.log(data,data=="1"); var val = $.trim(data); if(val == "1"){ $('#resurl').html('Success'); }else if (val == "0"){ $('#resurl').html('Failure'); }else{ $('#resurl').html('Unknown:[<span>'+val+'</span>]'); } }); });});</script></head><body><input id="id" /><div id="resurl"></div><input type="button" id="submit" /></html><? } ?> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:
Expert: SouthMod replied at 2024-11-10 03:46:57
mplungjan,
Would you like to reopen this question so that you can allocate points differently?
SouthMod
Experts Exchange Moderator
http://www.experts-exchange.com/R_25215.html
Would you like to reopen this question so that you can allocate points differently?
SouthMod
Experts Exchange Moderator
http://www.experts-exchange.com/R_25215.html
Author: Michel Plungjan replied at 2024-11-07 23:12:43
Robert's solution was first, but Chris' explanation was better.
thanks for pointing that out
thanks for pointing that out
Expert: duncanb7 replied at 2024-11-07 14:27:05
I think ChrisStanyon's answer to this thread is much completed and fast
by taking out php closing tab to solve mplungjan's issue completely
(no need to take care of how many space is left before ?>)
If I am wrong, I am sorry and please ignore my post
Duncan
by taking out php closing tab to solve mplungjan's issue completely
(no need to take care of how many space is left before ?>)
If I am wrong, I am sorry and please ignore my post
Duncan
Author: Michel Plungjan replied at 2024-11-07 14:01:35
Thanks for the participation.
I do not exactly see how the code Ray posted is any different than mine, however I understand now that the spurious space came from
} ?>_ <=== here
and that I could delete it or just do
} /* end of php */
which is what I did. Hence I accept this answer as the solution to my issue
Learned something new.
I do not exactly see how the code Ray posted is any different than mine, however I understand now that the spurious space came from
} ?>_ <=== here
and that I could delete it or just do
} /* end of php */
which is what I did. Hence I accept this answer as the solution to my issue
Learned something new.
Expert: Ray Paseur replied at 2024-11-07 09:26:36
Expert: Ray Paseur replied at 2024-11-07 06:30:14
In this post, line 22 opens a control structure, line 23 stops the PHP interpreter. Then some HTML is sent to the browser. PHP needs to know when the control structure ends. That is line 52. It starts PHP, provides the end of the control structure and then stops PHP.
If the "short open tag" is not set, you would need the complete <?php instead of just the <? to start the PHP interpreter.
I probably would have written this a little differently, but I was in a hurry and just working from what had been posted with the question.
If the "short open tag" is not set, you would need the complete <?php instead of just the <? to start the PHP interpreter.
I probably would have written this a little differently, but I was in a hurry and just working from what had been posted with the question.
Expert: Ray Paseur replied at 2024-11-07 06:17:04
<?php // RAY_temp_mplungjan.phperror_reporting(E_ALL);if(isset($_POST["id"])){ // FILTER / NORMALIZE THE REQUEST DATA $myid = trim($_POST['id']); // USING THE FILTERED DATA if($myid == "1") { echo "1"; } elseif($myid == "0") { echo "0"; } else { // UNKNOWN echo $myid; }}else {?><!DOCTYPE html><html><head><title>Post something</title><script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script><script type="text/javascript">$(document).ready(function(){ $('#submit').click( function(){ id = $("#id").val(); $.post('RAY_temp_mplungjan.php', {id:id}, function(data){ var val = $.trim(data); if(val == "1"){ $('#resurl').html('Success'); }else if (val == "0"){ $('#resurl').html('Failure'); }else{ $('#resurl').html('Unknown:[<span>' + val + '</span>]'); } }); });});</script></head><body><input id="id" /><div id="resurl"></div><input type="button" id="submit" /></html><? } ?> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:
HTH, ~Ray
Expert: Chris Stanyon replied at 2024-11-07 06:12:49
Actually, just an update to what I said - the space is after the closing php tag, not the one after the closing if bracket. One of the easiest ways to avoid this is to just leave off the closing PHP tag (but only if it's the end of your document). Finish your page like this:
Accepted Solution
Expert: Chris Stanyon replied at 2024-11-07 06:04:46
100 points EXCELLENT
@robert_schutt is bang on the money but beat me to posting. When you do an AJAX request the response is whatever the script outputs, and you have a space right at the end of your script (after the closing ?> tag). This also gets sent the to AJAX response.
If you're after checking against a number, then consider using parseInt(data)
If you're after checking against a number, then consider using parseInt(data)
Expert: Robert Schutt replied at 2024-11-07 05:55:49
PS: that could have been an unrelated issue, only in the post, but with that file I saw the problem and when I removed the space there, the problem disappeared.
Assisted Solution
Expert: Robert Schutt replied at 2024-11-07 05:53:56
400 points EXCELLENT
I copy/pasted the initially posted code into a file and there is actually a space as last character in the code, AFTER the last "}" of the else.
Author: Michel Plungjan replied at 2024-11-07 05:47:46
I changed to
window.console&&console.log(">>"+data+"<<",data=="1",data=="1 ");
>>1 << false true
So I do not understand what you mean by line 10?
window.console&&console.log(">>"+data+"<<",data=="1",data=="1 ");
>>1 << false true
So I do not understand what you mean by line 10?
Expert: duncanb7 replied at 2024-11-07 05:31:48
on line 10 on your first post
Author: Michel Plungjan replied at 2024-11-07 05:30:30
I do not understand what you mean.
I do not see any typing error.
I log data and the boolean data == "1" which is false since the data returned is "1 " and not "1"
I do not see any typing error.
I log data and the boolean data == "1" which is false since the data returned is "1 " and not "1"
Expert: duncanb7 replied at 2024-11-07 05:19:35
There is typing error, please re-run again
<?php //load.phpif(isset($_POST["id"])){ $myid = $_POST['id']; if($myid == "1"){ echo "1"; }else if($myid == "0"){ echo "0"; }}else {}?><!DOCTYPE html><html><head><title>Post something</title><script src="http://code.jquery.com/jquery-1.10.0.min.js"></script><script type="text/javascript">$(function() { $('#submit').click( function(){ $.post('load.php', {"id":$('#id').val()}, function(data){ window.console&&console.log(data,data=="1"); var val = $.trim(data); if(val == "1"){ $('#resurl').html('Success'); }else if (val == "0"){ $('#resurl').html('Failure'); }else{ $('#resurl').html('Unknown:[<span>'+val+'</span>]'); } }); });});</script></head><body><input id="id" /><div id="resurl"></div><input type="button" id="submit" /></html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:
window.console&&console.log("=="+data+"==",data=="1");
it show "1" and "0" not "1 " && "0 " if
using window.console&&console.log("=="+data+"==",data=="1");