Why doesn't this validate with the W3C validator:
3 variables from form.html going into form.php:
<?php
$stuff1 = $_POST["stuff1"];//catch variables
$stuff2 = $_POST["stuff2"];
$stuff3 = $_POST["stuff3"];
$myStuff[0] = $stuff1;//put into array
$myStuff[1] = $stuff2;
$myStuff[2] = $stuff3;
?>
Why doesn't this validate with the W3C validator:
You may be misunderstanding something here. PHP code is generated on the server side, and outputs HTML (or not). Your abovementioned script will not pass any HTML validator, because to the validator, it will be empty. PHP and the W3C validator have nothing to do with each other.
If you are getting a PHP error message, please post it.
It seems to be fine but try this too:
$stuff1 = $_POST["stuff1"];
$stuff2 = $_POST["stuff2"];
$stuff3 = $_POST["stuff3"];
$myarray = array();
$myarray[] = $stuff1;
$myarray[] = $stuff2;
$myarray[] = $stuff3;
print_r($myarray);
Also make sure that you put your fields inside a form eg form tag.
Note that php code is not something to be validated by the W3C validator, it is server-side generated code.
Try declaring your array:
$stuff1 = $_POST["stuff1"];//catch variables
$stuff2 = $_POST["stuff2"];
$stuff3 = $_POST["stuff3"];
$myStuff = array();
$myStuff[0] = $stuff1;//put into array
$myStuff[1] = $stuff2;
$myStuff[2] = $stuff3;
and note Pekka's answer about the difference between PHP (server-side) and HTML (client-side).
Try var_dump($_POST)
or a bit of validation
if (isset() && !empty($_POST["stuff1")) {
$myStuff[0] = $_POST["stuff1"];
} else {
echo '$_POST["stuff1"] error';
}
Check everything is being sent across correctly...
Related
I have two forms that sends data using ajax. Both forms have their own scripts and I thought that I would be able to access the same $_POST[] variables on the separate scripts, but this is not working. I tried using session_start() and include_once and for some reason I can't figure out why the variables are not passing from script to script. I've been at it for three days researching for a solution so if you know what I'm doing wrong or have an alternative please let me know thanks.
javascript.
$('.test-input').load("test.php", {
'sendTo':sendTo,
'carrier':carrier,
'testSubmit':testSubmit
})
$('#error-display').load("textsms.php", {
'date':scheduleDate,
'firstname':firstName,
'number':number,
'message':message,
'time':time,
'service':service,
'submit':submit
})
test.php
if (isset($_POST['testSubmit'])) {
$sendTo = $_POST['sendTo'];
$carrier = $_POST['carrier'];
$sendToInvalid = false;
$testEmpty = false;
$sendTo = "";
if (!preg_match('/^\(?\b\d{3}[-.)\s]?\s?\d{3}[-.)\s]?\d{4}\b$/', $sendTo) and $sendTo !== '') {
$sendToInvalid = true;
} elseif (empty($sendTo) || empty($carrier)) {
$testEmpty = true;
} else {
$sendTo = preg_replace('/[-.()\s]/','',$sendTo).$carrier;
$_SESSION['sendTo'] = $sendTo;
}
}
Trying to get $sendTo to pass to textsms.php down below.
else {
include 'test.php';
$sendTo = $_SESSION['sendTo'];
$number = preg_replace('/[-.()\s]/','',$number);
$number = "(".substr($number,0,3).") ".substr($number,3,3)."-".substr($number,6,4);
if ($sendTo !== "contactme#aboutryansam.com") {
$header = $name."\r\n#: ".$number;
$sendMsg = $userMsg."\r\nOn: ".$date.$time."\r\nService: ".$service;
//mail($sendTo, "You win!", $sendMsg, $header);
echo $sendTo;
echo "it worked";
}
Of course, on the page you initialize the ajax, include the "header.php" file that has inside session_start(). include_once('header.php') in the test.php as well. It should do the trick.
It's not a good practice nowadays, I better suggest you to use cookies and access them with $_COOKIE. Even better, use a library that takes care of cookies like this one. JQuery also has very good cookie management.
if(isset(file_get_contents("php://input"))) {
$credentialsXML = simplexml_load_string(file_get_contents("php://input"));
}
how can we check if php://input exists and then go ahead with parsing, as there are cases where php://input would be empty, and I simply don't need to call simplexml_load_string
Why don't you do:
$input = file_get_contents('php://input');
if (!empty($input)) {
$credentialsXML = simplexml_load_string($input);
}
I'm using smarty and php for my website. There is some code which converts the data into json. I want to use that data in a smarty template. That data is a set of error messages and I want to display those messages in a assigned smarty template at desired ID. I'm not able to get that in smarty template. Actually what is happening is the error messages are displayed on a plain page instead in the desired tag.
Following is my smarty and PHP code:
Following is the smarty code where I want to display the error messages:
if $error_msg}<div class="error-info">{$error_msg.error_msgs}</div>{/if}
Now following is my PHP code:
<?php
if($request['form_submitted']=='yes') {
$ret = $objPracticeSheet->InsertPracticeSheet($request, $practice_sheet_error_messages);
if(!$ret) {
$error_msg = $objPracticeSheet->GetAllErrors();
$data = array();
$data['error_message'] = $error_msg['error_msgs'];
$data = json_encode($data);
echo $data;
die;
} else {
$data = array();
$data['success_message'] = "success";
$data = json_encode($data);
echo $data;
die;
}
} else {
$all_subjects = $objSubjectsTopicsQues->GetAllSubjectsHavingTopics();
$smarty->assign('all_subjects', $all_subjects);
$smarty->assign('sheet_type', 'practice');
$bread_crumbs_text = 'Add Practice Sheet';
$submit_value = 'Submit';
$cancel_value = 'Cancel';
$file_to_show = 'manage-practice-sheet.tpl';
}
$smarty->assign("op", $op);
$smarty->assign("query_string", $query_string);
$smarty->assign("bread_crumbs_text", $bread_crumbs_text);
$smarty->assign("submit_value", $submit_value);
$smarty->assign("cancel_value", $cancel_value);
$smarty->assign("error_msg", $error_msg);
$smarty->assign("file_to_show", $file_to_show);
/*$smarty->assign('create', '-active');
$smarty->assign("sub_menu_file", "epn-create-sub-menu.tpl");
$smarty->assign('practice_sheet', '-active');*/
$smarty->assign('practice_sheet', 'active');
$smarty->assign('prepare', 'selected');
$smarty->display("index.tpl");
?>
Can you help me in displaying the error messages json data in the above div? Thanks in advance. I'm also attaching the screenshot of the current output.
Use JavaScript to parse the JSON, then put the errors in the div.
To make this easier, let's give your div an id:
<div id="error-info-main" class="error-info"></div>
Then
<script>
{literal}
(function() {
var error_json = {/literal}{$error_msg.error_msgs}{literal};
var errors = JSON.parse(error_json);
document.getElementById('error-info-main').innerHTML = errors.error_message;
}());
{/literal}
</script>
Alternatively, you could just the pass the message itself from PHP:
$smarty->assign("error_msg", $error_msg->error_message);
This may not be 100% correct, as I don't have your data to test with, but I believe that's right. You get the idea at any rate.
I got two php pages:
client.php and server.php
server.php is on my web server and what it does is open my amazon product page and get price data and serialize it and return it to client.php.
Now the problem I have is that server.php is getting the data, but when I return it and do echo after using unserialize(), it shows nothing. But if I do echo in server.php, it shows me all the data.
Why is this happening? Can anyone help me please?
This the code I have used:
client.php
$url = "http://www.myurl.com/iec/Server.php?asin=$asin&platform=$platform_variant";
$azn_data = file_get_contents($url);
$azn_data = unserialize($azn_data);
echo "\nReturned Data = $azn_data\n";
server.php
if(isset($_GET["asin"]))
{
$asin = $_GET["asin"];
$platform = $_GET["platform"];
echo "\nASIN = $asin\nPlatform = $platform";
//Below line gets all serialize price data for my product
$serialized_data = amazon_data_chooser($asin, $platform);
return($serialized_data);
}
else
{
echo "Warning: No Data Found!";
}
On server.php , you need to replace your following line:
return($serialized_data);
for this one:
echo $serialized_data;
because client.php reads the output of server.php, return is used to pass information from functions to caller code.
UPDATE:
Apart from the fixes above, you're hitting a bug in unserialize() function that presents with some special combination of characters, which your data seems to have, the solution is to workaround the bug by base64() encoding the data prior to passing it to serialize() , like this:
In client.php:
$azn_data = unserialize(base64_decode($azn_data));
In server.php:
echo base64_encode($serialized_data);
Source for this fix here .
You are not serializing your data on server side so there is nothing to deserialize on client side.
return(serialize($serialized_data));
Edit:
if(isset($_GET["asin"]))
{
$asin = $_GET["asin"];
$platform = $_GET["platform"];
echo "\nASIN = $asin\nPlatform = $platform";
//Below line gets all serialize price data for my product
$serialized_data = amazon_data_chooser($asin, $platform);
die(serialize($serialized_data));
}
else
{
echo "Warning: No Data Found!";
}
I wanted to post this online because I have been searching for days on this JQuery Remote validation issue. I cannot get it to work. I think my PHP code is correct as I have test the URL with a query in the URL and it returns false and true depending on with the recordset count is one or more
This is my Jquery Validate Code:
// validate form and submit
var $j = jQuery.noConflict();
$j(document).ready(function(){
$j("#myform").validate({
rules: {
ord_ref: {
required: true,
minlength: 12,
remote: "check_ord_ref.php"
},
messages: {
ord_ref: {
remote: "Order Number Does Not Exist"
}
}
}
});
});
This is my PHP code for the remote page "check_ord_ref.php"
$colname_rscheck_ord_ref = "-1";
if (isset($_GET['ord_ref'])) {
$colname_rscheck_ord_ref = (get_magic_quotes_gpc()) ? $_GET['ord_ref'] : addslashes($_GET['ord_ref']);
}
mysql_select_db($database_conn, $conn);
$query_rscheck_ord_ref = sprintf("SELECT ref_ord FROM orders WHERE ref_ord = '%s'", $colname_rscheck_ord_ref);
$rscheck_ord_ref = mysql_query($query_rscheck_ord_ref, $conn) or die(mysql_error());
$row_rscheck_ord_ref = mysql_fetch_assoc($rscheck_ord_ref);
$totalRows_rscheck_ord_ref = mysql_num_rows($rscheck_ord_ref);
if($totalRows_rscheck_ord_ref < 0){
$valid = 'false';
} else {
$valid = 'true';
}
echo $valid;
Please someone can you help solve the puzzle for myself and anyone else having issues
Using JQuery 1.5.2min
Validates OK without remote function
Ok, so I'm no PHP expert, but I do know that jQuery Validate expects the following result from a remote validation method:
The response is evaluated as JSON and must be true for valid elements,
and can be any false, undefined or null for invalid elements
Sending down "true" or "false" (note the quotation marks) is going to result in the value being parsed as the error message instead of being evaluated as a boolean primitive.
Back to the PHP part, I think you should probably use json_encode with a boolean primitive. I'm not quite sure the way to do this in PHP, but I believe it would be something like this:
$colname_rscheck_ord_ref = "-1";
if (isset($_GET['ord_ref'])) {
$colname_rscheck_ord_ref = (get_magic_quotes_gpc()) ? $_GET['ord_ref'] : addslashes($_GET['ord_ref']);
}
mysql_select_db($database_conn, $conn);
$query_rscheck_ord_ref = sprintf("SELECT ref_ord FROM orders WHERE ref_ord = '%s'", $colname_rscheck_ord_ref);
$rscheck_ord_ref = mysql_query($query_rscheck_ord_ref, $conn) or die(mysql_error());
$row_rscheck_ord_ref = mysql_fetch_assoc($rscheck_ord_ref);
$totalRows_rscheck_ord_ref = mysql_num_rows($rscheck_ord_ref);
if($totalRows_rscheck_ord_ref < 0){
$valid = false; // <-- Note the use of a boolean primitive.
} else {
$valid = true;
}
echo json_encode($valid);
This problem seems to be plaguing remote validation scripters and the jQuery documentation on the matter is clearly lacking.
I notice you are using jQuery 1.5.2: from what I understand (and found from experience) you must use the jQuery callback that is sent to the remote script with $_REQUEST with versions after 1.4, AND jQuery is expecting "true" or "false" as a STRING. Here is an example, confirmed working on multiple forms (I'm using jQuery 1.7.1):
if($totalRows_rscheck_ord_ref < 0){
header('Content-type: application/json');
$valid = 'false'; // <---yes, Validate is expecting a string
$result = $_REQUEST['callback'].'('.$check.')';
echo $result;
} else {
header('Content-type: application/json');
$valid = 'true'; // <---yes, Validate is expecting a string
$result = $_REQUEST['callback'].'('.$check.')';
echo $result;
}
I found this answer here (in the answers section), randomly, and have since stopped pulling out my hair. Hope this helps someone.
To add to Andrew Whitaker's response above, I must stress that you are sure that the response is strictly JSON and that there are no other content types being returned. I was having the same issue with my script, and everything appeared to be set properly - including using json_encode(). After some troubleshooting with Firebug's NET tab, I was able to determine that PHP notices were being sent back to the browser converting the data from JSON to text/html. After I turned the errors off, all was well.
//check_validate.php
<?php
// some logic here
echo json_encode(true);
?>