asked on
<?php
$array = split('[,]', $_POST['box_add']);
$boxerrortext = 'No duplicate boxes';
?>
<?php
if (isset($_POST['submit'])) {
$error = array();
foreach ($array as $box) {
$sql = "SELECT * FROM act WHERE item = '" . $box . "'";
$result = runSQL($sql) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows) {
//trigger_error('It exists.', E_USER_WARNING);
$error[] = array('boxerror'=>$boxerrortext,
'box'=>$box);
$result = json_encode($error);
echo $result;
return;
}
}
/* if ($box == 'DEMO111')
{
//echo 'There was an error somewhere';
//$box = 'ERROR';
//$error = array('boxerror'=>$boxerrortext, 'box'=>$box);
//$output = json_encode($error);
//echo $output;
//return;
} */
else {
$form = array();
foreach ($array as $box) {
$form[] = array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'destroydate'=>$destroydate,
'authorised'=>$authorised,
'submit'=>$submit);
$sql = "INSERT INTO `temp` (service, activity, department, company, address, user, destroydate, date, item, new) VALUES ('$service', '$activity', '$dept', '$company', '$address', '$requested', '$destdate', NOW(), '$box', 1)";
$result = runSQL($sql) or die(mysql_error());
}
}
}
$result=json_encode($form);
echo $result;
?>
ASKER
<?php
function runSQL($rsql) {
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "temp";
$connect = mysql_connect($hostname,$username,$password) or die ("Error: could not connect to database");
$db = mysql_select_db($dbname);
$result = mysql_query($rsql) or die (mysql_error());
return $result;
mysql_close($connect);
?>
submitHandler: function() {
if ($("#USRboxint").valid() === true) {
var data = $("#USRboxint").serialize() + '&submit=true';
$.ajax({
type: "POST",
url: "add.php",
data: data,
dataType: "json",
success: function(data) {
if(data.boxerror == 'No duplicate boxes'){
//$("#USRaddbox").html("<div class='errorMessage'>Sorry duplicates. Try again.</div>");
var $dialog = $('<div id="dialog"></div>').html('<br />Your New Intake of: ' + data.box + ' was NOT SUBMITTED.<br />You must enter a number that is unique<br />Thank you.');
$dialog.dialog({
autoOpen: true,
modal: true,
title: 'New Entry Unsuccessfull',
width: 400,
height: 200,
draggable: false,
resizable: false,
buttons: {
Close: function() {
$( this ).dialog( "close" );
}
}
});
} else {
//$("#USRaddbox").html("<div class='successMessage'>is a valid e-mail address. Thank you.</div>");
$("#USRboxint").get(0).reset();
var $dialog = $('<div id="dialog"></div>').html('<br /><b>Your New Intake of: ' + data.box + ' was successfully submitted.<br />Thank you.</b>');
$dialog.dialog({
autoOpen: true,
modal: true,
title: 'New Entry successfull',
width: 400,
height: 200,
draggable: false,
resizable: false,
buttons: {
Close: function() {
$( this ).dialog( "close" );
}
}
});
}
}
});
}
}
ASKER
ASKER
ASKER
ASKER
ASKER
$form[] = array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'destroydate'=>$destroydate,
'authorised'=>$authorised,
'submit'=>$submit);
$error = array();
$error[] = array( ...)
is going to result in
$error[0] =>
boxerror => 'Somevalue',
box => 'Some other value';
So when you refer to
success: function(data) {
data.boxerror
It is going to be a problem because you need to actually be doingsuccess: function(data) {
data[0].boxerror
if ($num_rows) {
$error[] = array(
'boxerror'=>$boxerrortext,
'box'=>$box
);
$result = json_encode($error);
echo $result;
return;
}
Toif ($num_rows) {
// SET $error to the array not the first element ($error[])
$error = array(
'boxerror'=>$boxerrortext,
'box'=>$box
);
$result = json_encode($error);
echo $result;
return;
}
ASKER
$company = mysql_real_escape_string($_POST['company']);
ASKER
ASKER
ASKER
ASKER
http://myserver/myphpscript.php?parameter1=value1¶meter2=value2
And see what the output is.http://myserver/myphpscript.php?parameter1=value1¶meter2=value2
But right at the top we have this, implying that you're using a POST-method request. $array = split('[,]', $_POST['box_add']);
You may be able to temporarily get around this by changing the back-end PHP script. You could use $_REQUEST or you could just write an assignment statement like this: $_POST = $_GET. Be aware that these strategies are hacks and must not be left in the deployed system; they create an unacceptable security exposure.ASKER
http://localhost/domain/users/boxrtvDB.php?requested=Some+User&activity=Box+Entry&service=Standard&rtv_dept=DEMO&address2=7-9+Some+Street+++London+W1D+000&box_rtv=demo111&status=9&company=DEMO&submit=Submit&submit=true
<?php echo "Hello World";
2. A request variable, sent from the browser address bar and returned in the browser window. This is where $_GET or $_REQUEST might come into play. For more details on which to choose, you need to understand that GET requests must be idempotent, whereas POST requests may change the data model. The $_REQUEST array may contain data from both kinds of requests, subject to the variables order directive.ASKER
<?php
// Connection config
function runSQL($rsql) {
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "sample";
$connect = mysql_connect($hostname,$username,$password) or die ("Error: could not connect to database");
$db = mysql_select_db($dbname);
$result = mysql_query($rsql) or die (mysql_error());
return $result;
mysql_close($connect);
}
?>
<?php
// test vars from jquery form
$status = mysql_real_escape_string($_REQUEST['status']);
$company = mysql_real_escape_string($_REQUEST['company']);
$requested = mysql_real_escape_string($_REQUEST['requested']);
$activity = mysql_real_escape_string($_REQUEST['activity']);
$address = mysql_real_escape_string($_REQUEST['address1']);
$service = mysql_real_escape_string($_REQUEST['service']);
$box = mysql_real_escape_string($_REQUEST['box_add']);
$authorised = mysql_real_escape_string($_SESSION['kt_name_usr']);
$dept = mysql_real_escape_string($_REQUEST['dept']);
$boxerrortext = 'Error';
// Split the box if multiples
$array = explode(',', $_REQUEST['box_add']);
$error = array();
// Loop to split if multiple request and check DB for dupe entries
foreach ($array as $box) {
$sql = "SELECT item FROM act WHERE item = '$box'";
$result = runSQL($sql) or die(mysql_error());
// If there are dupe entries, send message to jquery
if (mysql_num_rows($result)>0) {
//echo 'Error';
$error = array('boxerror'=>$boxerrortext, 'box'=>$box);
$output = json_encode($error);
echo $output;
return;
} else {
// If no dupes, then enter values into DB.
$form = array();
foreach ($array as $box) {
$form[] = array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'authorised'=>$requested);
$sql = "INSERT INTO `act` (service, activity, department, company, address, user, date, item, new)";
$sql .= "VALUES ('$service', '$activity', '$dept', '$company', '$address', '$requested', NOW(), '$box', 1)";
$result = runSQL($sql) or die(mysql_error());
}
}
}
$result=json_encode($form);
echo $result;
?>
ASKER
ASKER
$array = explode(',', $_REQUEST['box_add']);
file_put_contents('php_log.txt', print_r($array, true));
Array
(
[0] => demo111
[1] => fg91a
)
So it is working there. I then changed my original line:foreach ($array as $box) {
toforeach ($array as $box => $value) {
file_put_contents('php_log.txt', print_r($value, true));
foreach ($array as $box) {
// ***** HERE ****
file_put_contents('php_log.txt',printr_($value), FILE_APPEND);
$sql = "SELECT item FROM act WHERE item = '$box'";
$result = runSQL($sql) or die(mysql_error());
// If there are dupe entries, send message to jquery
if (mysql_num_rows($result)>0) {
// **** HERE *****
file_put_contents('php_log.txt',printr_($value), FILE_APPEND);
$error = array('boxerror'=>$boxerrortext, 'box'=>$box);
....
return;
} else {
// **** HERE ****
file_put_contents('php_log.txt',printr_($value), FILE_APPEND);
...
}
ASKER
<?php
$boxerrortext = 'Error';
// Split the box if multiples
$array = explode(',', $_REQUEST['box_add']);
$error = array();
// Loop to split if multiple request and check DB for dupe entries
foreach ($array as $box => $value) {
file_put_contents('php_log.txt',print_r($value), FILE_APPEND);
$sql = "SELECT item FROM act WHERE item = '$box'";
$result = runSQL($sql) or die(mysql_error());
// If there are dupe entries, send message to jquery
if (mysql_num_rows($result)>0) {
file_put_contents('php_log.txt',print_r($value), FILE_APPEND);
$error[] = array('boxerror'=>$boxerrortext, 'box'=>$box);
$output = json_encode($error);
echo $output;
return;
} else {
file_put_contents('php_log.txt',print_r($value), FILE_APPEND);
// If no dupes, then enter values into DB.
$form = array();
foreach ($array as $box) {
$form[] = array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'authorised'=>$requested);
$result=json_encode($form);
echo $result;
/* $sql = "INSERT INTO `temp` (service, activity, department, company, address, user, date, item, new)";
$sql .= "VALUES ('$service', '$activity', '$dept', '$company', '$address', '$requested', NOW(), '$box', 1)";
$results = runSQL($sql) or die(mysql_error()); */
}
}
}
?>
1111
foreach ($array as $box => $value) {
file_put_contents('php_log.txt','Foreach: ' . print_r($value, true) . "\n", FILE_APPEND);
$sql = "SELECT item FROM act WHERE item = '$box'";
$result = runSQL($sql) or die(mysql_error());
// If there are dupe entries, send message to jquery
if (mysql_num_rows($result)>0) {
file_put_contents('php_log.txt','If: ' . print_r($value, true) . "\n", FILE_APPEND);
$error[] = array('boxerror'=>$boxerrortext, 'box'=>$box);
$output = json_encode($error);
echo $output;
return;
} else {
file_put_contents('php_log.txt','Else: ' . print_r($value) . "\n", FILE_APPEND);
// I always have the ajaxError(function for development
$(document).ajaxError(function( event, request, settings, exc ) {
if (request.status==404) alert("ERROR from Ajax as '404 status' the "+settings.url+" page was NOT on Server, \nCan NOT recover from this ERROR, This operation is NOT available!");
else {
alert("ERROR from Ajax POST= Server-Status: "+request.status+", post-URL: "+settings.url+", post-Data: "+settings.data+", error because: "+exc);
if (request.responseText) {
$('#debug1').html(request.responseText);
}else $('#debug1').html("ajaxError responseText is empty");
}
});
ASKER
OHH, can't take it any moreReminds me of a scene in Airplane where people commit suicide rather than listen to Ted Straker :-) Anyway, on with the plot. I am getting error in line 42 of: Parse error: syntax error, unexpected '}'. Thanks ever so much for all your effort. Once we get this error sorted, I think we can move on.
ASKER
ASKER
PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.
TRUSTED BY
What is your jQuery code?
Confused by this
if (isset($_POST['submit']))
...are you using ajax? If so there is no $_POST['submit']