EDIT
Ive now got the following - but still it does not work
<script>
$(document).ready(function() {
$("button").click(function(){
var jsPostcode = document.login.getElementsByName("postcode").value;
var jsEmail = document.login.getElementsByName("email").value;
var formdata = {postcode:jsPostcode,email:jsEmail};
$.ajax(
{
type: "POST",
url: "database.php", //Should probably echo true or false depending if it could do it
data : formdata,
success: function(feed) {
if (feed!="true") {
// DO STUFF
} else {
console.log(feed);
// WARNING THAT IT WASN'T DONE
}
}}}};
</script>
=================================================================
ORIGINAL QUESTION FOLLOWS
I'm trying to take in data using a form and send some of this to a Mysql database. I cant use the action keyword in the form as that is being used to complete authentication therefore I am trying to use ajax and jquery. I have got to a point where I know I am close to cracking it but I'm not sure what is wrong. Please see my files below. First off the main file login.php is below:
Form Follows (login.php)
<?php
$mac=$_POST['mac'];
$ip=$_POST['ip'];
$username=$_POST['username'];
$linklogin=$_POST['link-login'];
$linkorig=$_POST['link-orig'];
$error=$_POST['error'];
$chapid=$_POST['chap-id'];
$chapchallenge=$_POST['chap-challenge'];
$linkloginonly=$_POST['link-login-only'];
$linkorigesc=$_POST['link-orig-esc'];
$macesc=$_POST['mac-esc'];
if (isset($_POST['postcode'])) {
$postcode = $_POST['postcode'];
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
?>
**SOME HTML HERE**
<script src="jquery-3.2.1.min.js"></script>
<script>
var js-postcode = document.login.getElementsByName("postcode").value;
var js-email = document.login.getElementsByName("email").value;
var formdata = {postcode:js-postcode,email:js-email};
$("button").click(function(){
$.ajax(
{
type: "POST",
url: "database.php", //Should probably echo true or false depending if it could do it
data : formdata,
success: function(feed) {
if (feed!="true") {
// DO STUFF
} else {
console.log(feed);
// WARNING THAT IT WASN'T DONE
}
}}}
</script>
</head>
<body>
<table width="100%" style="margin-top: 10%;">
<tr>
<td align="center" valign="middle">
<table width="240" height="240" style="border: 1px solid #cccccc; padding: 0px;" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="bottom" height="175" colspan="2">
<!-- removed $(if chap-id) $(endif) around OnSubmit -->
<form name="login" action="<?php echo $linkloginonly; ?>" method="post" onSubmit="return doLogin()" >
<input type="hidden" name="dst" value="<?php echo $linkorig; ?>" />
<input type="hidden" name="popup" value="true" />
<table width="100" style="background-color: #ffffff">
<tr><td align="right">login</td>
<td><input style="width: 80px" name="username" type="text" value="<?php echo $username; ?>"/></td>
</tr>
<tr><td align="right">password</td>
<td><input style="width: 80px" name="password" type="password"/></td>
</tr>
<tr><td align="right">Postcode</td>
<td><input style="width: 80px" name="postcode" type="text" /></td>
</tr>
<tr><td align="right">Email</td>
<td><input style="width: 80px" name="email" type="text" /></td>
</tr>
<td><button><input type="submit" value="OK" /></button></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</td>
</tr>
</table>
<script type="text/javascript">
<!--
document.login.username.focus();
//-->
</script>
</body>
</html>
and called file database.php is as follows:
<?php
if ((isset($_POST['postcode'])) && (isset($_POST['email']))) {
$postcode = $_POST['postcode'];
$email = $_POST['email'];
$connect= new mysqli_connect('xx','xx','xx','xx');
if ($conn->connect_errno) {
echo "There was a problem connecting to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
}
if (!($sql = $conn->prepare("INSERT INTO visitors(postcode,email) VALUES(postcode,email)"))) {
echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
}
//NOTE: the "ss" part means that $postcode and $email are strings (mysql is expecting datatypes of strings). For example, if $postcode is an integer, you would do "is" instead.
if (!$sql->bind_param("ss", $postcode, $email)) {
echo "Binding parameters failed: (" . $sql->errno . ") " . $sql->error;
}
if (!$sql->execute()) {
echo "Execute failed: (" . $sql->errno . ") " . $sql->error;
}
} else {
echo 'Variables did not send through ajax.'; // any echoed values would be sent back to javascript and stored in the 'response' variable of your success or fail functions for testing.
}
?>
Please help - all assistance greatly appreciated
I stuck with this problem for a few days already.
The logic of the system i want to develop is here
This is the code for the image
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- <link rel="stylesheet" type="text/css" href="style.css"/> -->
</head>
<body>
<form action="process/searchprocess.php" method="GET">
<table width="100%">
<tr>
<th>
Client Ic<br><input type="text" name="client_name" />
</th>
<th>
Client Ic<br><input type="text" name="client_ic" />
</th>
<th>
Client Address <br><input type="text" name="client_add" />
</th>
</tr>
</table>
<table width="100%">
<tr>
<th>
<br><input type="submit" value="Search" align="center" />
</th>
</tr>
</table>
</form>
</body>
</html>
This is my process code
<?php
session_start();
mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error());
mysql_select_db("waveevo") or die(mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Search results</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- <link rel="stylesheet" type="text/css" href="style.css"/> -->
<style>
table tr:nth-child(even) {
background-color: #eee;
}
table tr:nth-child(odd) {
background-color:#fff;
}
table th {
background-color: black;
color: white;
}
</style>
</head>
<body>
<?
php
$query = $_GET['client_name'];
$query2 = $_GET['client_ic'];
$query3 = $_GET['client_add'];
if ($query == null && $query2 == null && $query3 == null)
{
echo "Please at least insert one the value";
}
else
{
$query = htmlspecialchars($query);
$query2 = htmlspecialchars($query2);
$query3 = htmlspecialchars($query3);
$query = mysql_real_escape_string($query);
$query2 = mysql_real_escape_string($query2);
$query3 = mysql_real_escape_string($query3);
$raw_results = mysql_query("SELECT * FROM client WHERE ('client_name' LIKE '%".$query."%') OR ('client_ic' LIKE '%".$query2."%') OR ('client_add_1' && ' ' && 'client_add_2' && ' ' && 'client_add_3' && ' ' && 'client_add_4' LIKE '%".$query3."%')") or die(mysql_error());;
if(mysql_num_rows($raw_results) > null){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
?>
<table width="100%">
<tr>
<th>ID</th>
<th>Name</th>
<th>IC</th>
<th>Mobile</th>
<th>Address</th>
<th>Marital Status</th>
<th>Race</th>
<th>Asset Type</th>
<th>Bank</th>
<th>Amount</th>
<th>Nationality</th>
<th>Limit</th>
</tr>
<tr>
<td><?php echo $results["client_id"]; ?></td>
<td><?php echo $results["client_name"]; ?></td>
<td><?php echo $results["client_ic"]; ?></td>
<td><br><?php echo $results["client_mobile_1"]."<br>".$results["client_mobile_2"]."<br>".$results["client_mobile_3"]; ?></td>
<td><?php echo $results["client_add_1"]."<br>".$results["client_add_2"]."<br>".$results["client_add_3"]."<br>".$results["client_city"]."<br>".$results["client_postcode"]; ?></td>
<td><?php echo $results["client_marital_status_id"]; ?></td>
<td><?php echo $results["client_race_id"]; ?></td>
<td><?php echo $results["client_asset_type_id"]; ?></td>
<td><?php echo $results["client_bank_id"]; ?></td>
<td><?php echo $results["amount"]; ?></td>
<td><?php echo $results["client_nationality_id"]; ?></td>
<td><?php echo $results["client_limit"]; ?></td>
</tr>
</table>
<?php
}
}
else{ // if there is no matching rows do following
echo "No results";
}
}
?>
</body>
So I try enter value for client ic, for example 1234, there is no data in the database that match the value that i entered just now but the result still show have, can i know why because i already don't have way to solve this
ThatÅ› because when you use wildcards LIKE '%".$query."%' if your variable $query is empty you are just getting all because you compare with everything LIKE '%%'.
You need to change this sentence:
$raw_results = mysql_query("SELECT * FROM client WHERE ('client_name' LIKE '%".$query."%') OR ('client_ic' LIKE '%".$query2."%') OR ('client_add_1' && ' ' && 'client_add_2' && ' ' && 'client_add_3' && ' ' && 'client_add_4' LIKE '%".$query3."%')") or die(mysql_error());;
whith something like this
$sql_query="SELECT * FROM client WHERE ";
$other=false;
if ($query != null and $query!="") {
$sql_query=$sql_query."('client_name' LIKE '%".$query."%')";
$other=true;
}
if ($query2 != null and $query2!="") {
if ($other) {
$sql_query=$sql_query." OR ";
}
$sql_query=$sql_query."('client_ic' LIKE '%".$query2."%')";
$other=true;
}
if ($query3 != null and $query3!="") {
if ($other) {
$sql_query=$sql_query." OR ";
}
$sql_query=$sql_query."('client_add_1' && ' ' && 'client_add_2' && ' ' && 'client_add_3' && ' ' && 'client_add_4' LIKE '%".$query3."%')";
$other=true;
}
if ($other) {
$raw_results = mysql_query($sql_query) or die(mysql_error());
}
<script>
function removeAllRowsContainingCheckedCheckbox() {
alert("hello");
for (var rowi= table.rows.length; rowi-->0;) {
var row= table.rows[rowi];
var inputs= row.getElementsByTagName('input');
for (var inputi= inputs.length; inputi-->0;) {
//alert("inside for");
var input= inputs[inputi];
if (input.type==='checkbox' && input.checked) {
//alert("indide if ")
row.parentNode.removeChild(row);
break;
}
}
}
}
</script>
<html>
<head>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<table border="1" id="table">
<tr>
<td colspan="2">Select Technolgy:</td>
</tr>
<tr>
<td>c</td>
<td><input type="checkbox" name="techno[]" ></td>
</tr>
<tr>
<td>hadoop</td>
<td><input type="checkbox" name="techno[]" ></td>
</tr>
<tr>
<td>core java</td>
<td><input type="checkbox" name="techno[]" ></td>
</tr>
<tr>
<td>Javascript</td>
<td><input type="checkbox" name="techno[]" ></td>
</tr>
<tr>
<td>springs</td>
<td><input type="checkbox" name="techno[]" ></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="submit" name="sub"></td>
</tr>
<input type='button' value='del' onclick='removeAllRowsContainingCheckedCheckbox();'>Click me to delete
</table>
</form>
<?php
if(isset($_POST['sub']))
{
$db = pg_connect("host=localhost port=5432 dbname=postgres user=postgres password=ndem$123");
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
$checkbox1=$_POST['techno'];
$chk="";
foreach($checkbox1 as $chk1)
{
$chk .= $chk1."";
echo '<script>alert("Inserted Successfully")</script>';
}
$in_ch=pg_query("insert into news_table(technology) values ('$chk')");
echo "the check box value is " . $in_ch;
if($in_ch==1)
{
echo '<script>alert("Inserted Successfully")</script>';
echo "Records created successfully\n";
}
else
{
echo pg_last_error($db);
}
pg_close($db);
}
?>
</body>
</html>
For the submit can i able to write javascript function with out using
$checkbox1=$_POST['techno'];
$chk="";
foreach($checkbox1 as $chk1)
{
$chk .= $chk1."";
echo '<script>alert("Inserted Successfully")</script>';
}
above code because if(isset($_POST['sub'])) is reloading the already submitted checkboxs in the table so user may confuse after delete the page will again show all the check box those who are submitted also .. Please suggest alternatives for this any ways i want to insert the checked checkboxes into database but my thought is to delete the checked submitted also in the table.
I have the following javascript being included in some PHP which I am writing. The problem is that it is not working and developer tools keeps saying "'validate_entry' is undefined" but I don't know what I'm doing
My code is:
<script type="text/javascript">
function validate_entry()
{
var name=document.getElementById("submitted").value;
var system=document.getElementById("system").value;
var details=document.getElementById("description").value;
if(name.trim() == "")
{
alert("The name you entered is too short");
return false;
}
if(name.length > 64)
{
alert("The name you entered is too long");
return false;
}
if(system.trim() == ""
{
alert("System is not valid");
return false;
}
if(details == "")
{
alert("Description cannot be empty");
return false;
}
}
</script>
and I have the following form:
<form action="index.php" method="POST" onsubmit="return validate_entry();">
<table>
<tr>
<td><label for="submitted">Issue Submitted By</label></td>
<td><input type="text" name="submitted" id="submitted" tabindex="<?php echo $header[0]++; ?>"></td>
</tr>
<tr>
<?php $systems = get_system_names($connection);
if($systems==FALSE)
{
echo "<td></td>\n";
echo "<td></td>\n";
}
else
{
echo "<td><label for=\"system\">System Name</label></td>\n";
echo "<td><select name=\"system\" id=\"system\" tabinex=\"". $header[0]++. "\">\n";
echo "<option>Please Select</option>\n";
foreach($systems as $system)
{
echo "<option value=\"{$system[0]}\">{$system[1]}</option>\n";
}
echo "</select></td>\n";
}
?>
</tr>
<tr>
<td><label for="description">Issue Description</label></td>
<td><textarea name="description" id="description" tabindex="<?php echo $header[0]++; ?>"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" id="submit" tabindex="<?php echo $header[0]++; ?>"></td>
</tr>
</table>
<?php echo "</form>\n";
Make sure the positions of the JavaScript is after the html code
Try to test the JavaScript function using browser console
If it did work try to submit and handle other errors :)
I have a listbox that displays a couple of internships under following format
id - name :
1 - Computer Science
So far, I have create the function addRow in order to update my fields from form.
If I do
alert($montext)
I can display "1 - Computer Science", but I am looking only for the value "1".
I tried :
alert(<?php substr($montext,0,2)?>);
But seems that php inside "script" isn't being executed.
Because following code changes the value in the field:
document.getElementById('ti').value=$montext;
Because I'd like also to execute php code inside the script TAG.
I'm running under Apache.
If you could help me out. Thanks
Find hereby the used code.
<html>
<head>
<script>
function addRow(title,text,description,id) {
$montext=$( "#idStage option:selected" ).text();
alert($montext);
document.getElementById('ti').value=$montext;
/*document.getElementById('te').value=text;
document.getElementById('de').value=description;
document.getElementById('id').value=id;*/
}
function setText(title,text,description,id){
document.getElementById('title').value=title;
document.getElementById('text').value=text;
document.getElementById('description').value=description;
document.getElementById('id').value=id;
}
</script>
</head>
<body>
<?php
include('../admin/connect_db.php');
?>
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<label class="notBold">Choose the internship you want to update: </label>
<select name="idStage" id="idStage" onChange="addRow()">
<?php
$stmt = $db->stmt_init();
if($stmt->prepare('SELECT id, title,text,description FROM offre ORDER by published_date ASC')) {
$stmt->bind_result($id,$title,$text,$description);
$stmt->execute();
while($stmt->fetch()){
?>
<option id="nostage" value="<?php echo$id;?>" onclick="setText('<?php echo $title ?>',' <?php echo $text ?> ',' <?php echo $description ?>',' <?php echo $id?>');"><?php echo $id." - ".$title;?></option>
<?php
}
$stmt->close();
}
?>
</select>
</td>
<td width="20">
<img src="./Image/exit.png" title="Close" id="closeDelete" class="closeOpt" onclick="closeOpt()" />
</td>
</tr>
</table>
<form method="post" action="modifystage.php">
<table>
<tr>
<td>
<input type = "hidden" id ="id" name="id"/>
</td>
</tr>
<tr>
<td class="label">
<label>Title </label>
<textarea id = "ti" name="ti" rows = "3" cols = "75">
<?php
$stmt = $db->stmt_init();
if($stmt->prepare('SELECT id, title,text,description FROM offre ORDER by published_date ASC')) {
$stmt->bind_result($id,$title,$text,$description);
$stmt->execute();
}
echo $title;
?>
</textarea>
</td>
</tr>
<tr>
<td class="label">
<label>Desc</label>
<textarea id = "de" name="de" rows = "3" cols = "75">
<?php
$stmt = $db->stmt_init();
if($stmt->prepare('SELECT id, title,text,description FROM offre ORDER by published_date ASC')) {
$stmt->bind_result($id,$title,$text,$description);
$stmt->execute();
}
echo $description;
?>
</textarea>
</td>
</tr>
<tr>
<td class="label">
<label>Text </label>
<textarea id = "te" name="te" rows = "3" cols = "75">
<?php
$stmt = $db->stmt_init();
if($stmt->prepare('SELECT id, title,text,description FROM offre ORDER by published_date ASC')) {
$stmt->bind_result($id,$title,$text,$description);
$stmt->execute();
}
echo $text;
?>
</textarea>
</td>
</tr>
<tr>
<td colspan="2" align="right"colspan="2" class="label">
<button type="submit">Submit</button>
</td>
</tr>
</table>
</form>
</body>
</html>
You don't need to use PHP here. Use the javascript substring function - http://www.w3schools.com/jsref/jsref_substring.asp
For example
alert(montext.substring(0, 2));
Write your <script> on bellow your PHP and try this :
<script>
function addRow(title,text,description,id) {
var montext = $( "#idStage" ).text();
alert(montext);
document.getElementById('ti').value(montext);
/*document.getElementById('te').value=text;
document.getElementById('de').value=description;
document.getElementById('id').value=id;*/
}
function setText(title,text,description,id){
document.getElementById('title').value=title;
document.getElementById('text').value=text;
document.getElementById('description').value=description;
document.getElementById('id').value=id;
}
</script>
If you want to call variable from PHP, don't forget to use echo like this :
alert("<?php echo substr($montext,0,2); ?>");