Ajax Ignoring SQL - php

I have other functions just like this that work, however when I click, the Ajax gets posted but my page would originally refresh, until I added event.preventDefault. I have been stumped for hours and I cant see the problem..
Here is my AJAX:
$(function(){
$(document).on('click','.addnewproject',function(){
event.preventDefault();
var curaid= $(this).attr('id');
var $ele = $(this);
$.ajax({
type:'POST',
url:'components/sql/insertproject.php',
data:$("#addnewprojecttable").serialize(),
success:
function(data){
$(".image-list" + curaid).append(
'<li id="projectfolder' + curaid + '">\n\
<img width="35px" onclick="openproject('+ curaid +')" style="cursor: pointer;" src="img/folder.png" /> \n\
<p>Project ' + curaid + '</p> \n\
');
}
});
});
});
My HTML:
<form id="addnewprojecttable" method="post" >
<table>
<tr>
<td>
<input style="display:none;" name="username" id="username" value="<?php echo "" . $_SESSION['username']; ?>" type="text"/>
<input style="float:left; width:100%; height:20px; margin-bottom: 20px;" name="projectname" id="projectname" type="text" placeholder="projectname"/>
<input style="float:left; width:100%; height:20px; margin-bottom: 20px;" name="contactname" id="contactname" type="text" placeholder="contactname"/>
<input style="float:left; width:100%; height:20px; margin-bottom: 20px;" name="contactemail" id="contactemail" type="text" placeholder="contactemail"/>
<input style="float:left; width:100%; height:20px; margin-bottom: 20px;" name="contactphone" id="contactphone" type="text" placeholder="contactphone"/>
<input style="float:left; width:100%; height:20px;" name="description" id="description" type="text" placeholder="description"/>
<input style="float:left; width:100%; height:20px;" name="notes" id="notes" type="text" placeholder="notes"/>
<button class="addnewproject" id="<?php echo $row["accid"]; ?>" >Add</button>
<button onclick="closenewprojectwindow()">Cancel</button>
</td>
</tr>
</table>
</form>
My SQL:
<?php
include 'config.php';
$sqlupdateincome = "INSERT INTO projects (username, projectname)
VALUES ('". $_POST['username'] ."', 'Ochrom Test Project')";
if ($conn->query($sqlupdateincome) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Here is my database just in case it might be that. click for image
I do appreciate all help and responses, thank you!

Lets break this down. You have a form tag, which means you want to submit a form. Without an action attribute, you will be at the will of the browser, but pretty much all will submit the form the same page.
<form id="addnewprojecttable" method="post">
Further down, still within the form, you have a button. The default action of a button is to submit the form unless you have modified the function of the button with Javascript, which is what you kind of have.
<button class="addnewproject" id="<?php echo $row["accid"]; ?>" >Add</button>
I do NOT use JQuery at all, but I can read it enough to understand what you are doing. Your modification code for the button is saying. Do NOT submit the form with this line:
event.preventDefault();
But then continue with the rest of the code in the function.
Without this line, the rest of the function will still be actioned and the form will be submit to itself as well (default action of the HTML). It may also be possible that the browser does not finish actioning the Javascript function before the form submission (inconsistent results - not a good thing).
Depending on your layout, sometimes you may need the following, but that is really another discussion.
event.stopPropagation();
event.preventDefault();
It is good practice to allow your HTML to work without Javascript enabled as well. For this reason, including an action in your form would also be something I would recommend.
Take note: Your SQL is open to SQL Injection as well.
Edit:
What is the full path to config.php and components/sql/insertproject.php?
On insertproject.php, add the following to the top:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
And then happens when you go directly to the URL /components/sql/insertproject.php? Is there an error displayed?

Related

Carry POST through html hyperlink

I have a hyperlink as shown:
<div style="clear:both"> <a color="grey" accesskey=""style="float: right" href="newbattle.php? userid= <?php echo $id0; ?>"> [<font color="grey">Attack</font>]</a><br></div> <br>
Is it possible, using only only php, to carry POST data? I want to put this
<input type="hidden" name="test" value="<?php echo $number;?>
So I can $_POST['test'] on the other page and get the $number. I can switch over to normal form but I really like what I have
No, that's not possible. If you want to submit a POST request, you should go through a <form> and submit it.
You cannot post through a hyperlink, unless you use JavaScript to capture the click event and simulate a click on a submit button.
But a better approach, I think, would be to make an actual submit button. With a bit of CSS you can style that button to look as if it was a hyperlink. That way, if the CSS fails, you've still got a working button, while if a JavaScript issue would occur, you have a disfunctional link with unexpected behaviour.
input[type=submit] {
display: inline;
border: none;
background-color: transparent;
color: blue;
cursor: pointer;
}
input[type=submit]:hover {
text-decoration: underline;
}
<form action="otherpage.php" method="POST">
<input type="hidden" name="test" value="<?php echo $number;?>">
<input type="submit" value = "Look, I'm a link!">
</form>
A link redirects user to another page, it's purpose is not for get/post requests.
If you want to send a post request on a click, you can do it with a submit button inside form. For example,
<form action="another_page.php" method="POST">
<input type="hidden" name="test" value="<?php echo $number;?>
<input type="submit" />
</form>
Style the button like a hyperlink and it will send a post request as expected.
You can do that using a form.
When the user clicks the link, the form is submitted with the "test" variable and the "userid" variable.
Here's the code:
<form method="post" action="newbattle.php" id="myForm">
<div style="clear:both">
<a color="grey" accesskey="" style="float:right;" href="" onclick="javascript:document.myForm.submit(); return false;">[<font color="grey">Attack</font>]</a>
<br/>
</div>
<br/>
<input type="hidden" name="userid" value="<?php echo $id0; ?>" />
<input type="hidden" name="test" value="<?php echo $number; ?>" />
</form>
For my specific problem, here's what I ended up doing.
href="newbattle.php?userid= <?php echo $id0; ?>&num=<?php echo $number; ?>"
I added the $number on to the hyperlink and then retrieved it with $_GET on the next page
If you want to access test value by $_POST you have to use form like this :
<form action="another_page.php" method="POST">
<input type="hidden" name="test" value="<?php echo $number;?>
<input type="submit" />
</form>
get.php :
<?php
$num = $_POST['test'];
echo $num;
?>

different form action based on two buttons

The form that I'm trying to work has two buttons:
1: for viewing the submitted information and
2: for saving the confirmed information.
Part of my form:
$sql="INSERT INTO applicant_information
(entrepreneur_name,enterprise_name,.....) values
('".$_POST['entrepreneur_name']."','".$_POST['enterprise_name']."','".$_POST['address']."'...)
<form method="post" action="business_form.php">
<table width="70%" cellspacing="2px" cellpadding="5px"style="border:1px solid black;border-collapse:collapse;">
<th colspan="8"align="left" style="border:1px solid black;"><b>Personal
Information</th>
<tr>
<td width="18" rowspan="2" style="border:1px solid black;">1</td>
<td width="142" rowspan="2"style="border:1px solid black;" >Name</td>
<td style="border:1px solid black;" colspan="2">Entrepreneur</td>
<td colspan="2"style="border:1px solid black;"><?php echo $_POST['entrepreneur_name']?>
<input id="entrepreneur_name" name="entrepreneur_name" type="hidden" value="<?php echo $_POST['entrepreneur_name']?>" />
</td>
</tr>.....
//rest of the form
<input type="submit" name="edit" style="width:10%"value="Back to edit" />
<input type="submit" name="reg"style="width:10%"value="Submit" />
What I'm trying to do is to run the query when the user hit the submit button. Any idea how to do that?
What I usually do is just have one button change the form's destination on click, then submit it. So for example:
<form action="login.php" method="POST" id="myform">
<input name="username">
<input type="password" name="password">
<input type="submit" value="Login">
<button id="js-register">Register</button>
</form>
With
$('#js-register').click(function() {
$('#myform').attr('action', 'register.php').submit();
});
Or you could have both buttons be Javascript'd and bind both of them for consistency's sake - up to you.
HTML
<form action="handle_user.php" method="POST />
<input type="submit" value="View" name="view" />
<input type="submit" value="Submit" name="submit">
</form>
Check condition in php as following way...
if($_POST["view"]) {
//User hit the view button, handle accordingly
}
if($_POST["submit"]) {
//User hit the Submit information , handle accordingly
}
You need to track the button named "reg". So right after the $sql string, you can put the following:
<?php
if (isset($_POST['reg'])) {
mysql_query($sql);
if (mysql_affected_rows() > 0) {
echo "REgistration completed";
}
else {
echo "System could not process the registration";
}
}
?>
Hope that will help you.
You could make the "edit" be a plain button, instead of a submit type. And bind a click event to it, which could either redirect to the editable form or make the form editable (which ever suits you best). Then the "reg" submit could work as it does currently to save the data.

creating session for simple php password

I need to be able to store the password submitted in a session, as at present using the php_self method the page is just reloading as if the user has not entered the password correctly (this is happening in Firefox, it actually works fine with no session in Chrome / Safari) and so I believe a session is needed for it to work in FF, but I'm newish to PHP and not sure how I can go about this, here's the code:
Thanks in advance
<?php
session_start();
$Password = 'hello';
if (isset($_POST['submit_pwd'])){
$pass = isset($_POST['passwd']) ? $_POST['passwd'] : '';
if ($pass != $Password) {
showForm("error", "WRONG PASSWORD");
exit();
}
} else {
showForm();
exit();
}
function showForm($Inputclass="mister", $Placeholder="PLEASE ENTER PASSWORD"){
?>
With HTML:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="pwd" id="pwd">
<input class="<?php echo $Inputclass; ?>" name="passwd" type="password" size="79" style="margin-left: -100px;" placeholder="<?php echo $Placeholder; ?>"/>
<br/>
<input class="text" name="submit_pwd" value="Login" type="image" src="loginkey.png" style="position: relative; left: 240px; top: -35px;">
</form>
If you hit the button, then $_POST['submit_pwd_x'] and $_POST['submit_pwd_y'] should be set, not $_POST['submit_pwd']
If you hit "enter" then $_POST['submit_pwd'], $_POST['submit_pwd_x'] or $_POST['submit_pwd_y'] should NOT be set - but sometimes are depending on the browser.
You can't rely on either, so your check if (isset($_POST['submit_pwd'])) will be unreliable
The trick is to add a hidden field in the form, and check for sbmission of that hidden field in $_POST.
More details on the answer (based on comment):
PHP is
if (isset($_POST['formsubmitted'])){
....
}
HTML is
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="pwd" id="pwd">
<input class="<?php echo $Inputclass; ?>" name="passwd" type="password" size="79" style="margin-left: -100px;" placeholder="<?php echo $Placeholder; ?>"/>
<br/>
<input type="hidden" name="formsubmitted" value="1" />
<input class="text" name="submit_pwd" value="Login" type="image" src="loginkey.png" style="position: relative; left: 240px; top: -35px;">
</form>
Edit:
If you don't click on the button (hit enter):
IE will send none
FF,Opera will send the submit_pwd_x=0 and submit_pwd_y=0
Chrome,Safari will send the submit_pwd_x=0, submit_pwd_y=0 and submit_pwd
If you click the button:
IE will send submit_pwd_x=X and submit_pwd_y=Y
FF,Opera will send submit_pwd_x=X and submit_pwd_y=Y
Chrome,Safari will send the submit_pwd_x=X, submit_pwd_y=Y and submit_pwd

HTML forms, add area on click

I'm not sure how to ask what I'm looking for to search how-tos. I am building a form and one of the entries requires users to list an appliance, its voltage, watts, amps, phase, etc.
I'd like a simple row with "X" columns providing the text areas for one appliance and then the ability to click a link to 'add another appliance' using jquery/html.
I like using placeholder text to save space on the page. I can get all this set up just fine for a single entry like 'name' however I don't know how to implement an 'add entry' row. All of the data is stored via PHP in MySQL.
So A: What is the name of this type of form section. B: What is it called when we want to let the user add a row to this section?
I love making things harder than they really are. It's my specialty. I guess :)
EDIT: http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_submit
Using this format with 5 columns per entry (though it will all be on one line/row) I'd like to have an "add entry" link which generates a new blank entry option.
#elecNeeds input[type=text], textarea {
font-size: 12px;
font-style: italic;
width: 15%;
height: 20px;
padding: 10px;
color: #212323;
background: #E3E3E3;
background: rgba(255, 255, 255, 0.5);
border: 2px #000 solid;
margin-bottom: 10px;
position: relative;
behavior: url(/js/PIE.htc);
}
<div id="elecNeeds">
<input type="text" name="appliance" placeholder="Type of Equipment">
<input type="text" name="voltage" placeholder="Voltage">
<input type="text" name="watts" placeholder="Watts">
<input type="text" name="amps" placeholder="Phase">
<input type="text" name="notes" placeholder="Notes">
<br /> Add an appliance
</div>
I don't know what's it called, but you probably want this - http://jsfiddle.net/uPWkf/1/
<form method="post" action="#" id="myForm">
<div id="nameFields">
<label>Watt <input type="text" name="watt0" /></label>
<label>Volt <input type="text" name="volt0" /></label>
<label>Amp <input type="text" name="amp0" /></label><br/><br />
</div>
<input type="submit" value="submit" id="submit" />
</form>
Add New Row
and the JS
var i = 1;
$("#addRow").click(function() {
$("#nameFields").append('<label>Watt <input type="text" name="watt' + i + '" /></label><label>Volt <input type="text" name="volt' + i + '" /></label><label>Amp <input type="text" name="amp' + i + '" /></label><br/><br />');
i++;
});
$('#myForm').submit(function() {
var values = $('#myForm').serialize();
alert(values);
});
I think You need to use $(selector).append('<code>'); function. For example:
Add
<table class="my_fuits">
<tr>
<td>Fruit</td>
<td><input type="text" name="fuits[]" /></td>
</tr>
</table>
and js(jQuery) code:
$(document).ready(function(){
// add one more row
$(".add").live('click',function(){
$(".my_fuits").append('<tr><td>Fruit '+$(".my_fruits input").length+'</td><td><input type="text" name="fuits[]" />[X]</td></tr>');
return false;
});
// remove row
$(".remove").live('click',function(){
$(this).parent().parent().remove();
return false;
});
});

JQuery/Javascript select-all by clicking link or selecting "select-all" from a checkbox dropdown

I have a private messaging system and would like to have about 4-5 links above the inbox where a user can click if they want to "select all", "none", "favourite", "read" or "unread" messages.
How would I do this using jquery/javascript? Are there any tutorials about that explain this thoroughly? I am not that great with javascript but I'm a quick learner.
I originally really wanted to do a gmail style checkbox drop down but it is proving to be quite difficult and I think having links across of the message inbox would be more user friendly..
I hacked together a simple example of how I would do this, you will have to tailor it to your needs, but it should get you started.
Just try this (make sure you change it to match your jquery library):
<html>
<body>
<div class="mesg" id="mesg1" read="1" favorite="0">
<input type="checkbox" name="check1">
message 1 info
</div>
<div class="mesg" id="mesg2" read="1" favorite="1">
<input type="checkbox" name="check2">
message 2 info
</div>
<div class="mesg" id="mesg3" read="0" favorite="0">
<input type="checkbox" name="check3">
message 3 info
</div>
<input type="button" value="select read" id="select_read" />
<input type="button" value="select favorite" id="select_fav" />
<script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function() {
$("#select_read").click(function() {
$("div.mesg input[type=checkbox]").attr("checked", false);
$("div.mesg[read=1] input[type=checkbox]").attr("checked", true);
});
$("#select_fav").click(function() {
$("div.mesg input[type=checkbox]").attr("checked", false);
$("div.mesg[favorite=1] input[type=checkbox]").attr("checked", true);
});
});
</script>
</body>
</html>
My solution on jsFiddle: http://www.jsfiddle.net/pereskokov/sYe4S/6/
HTML:
<p id="links">
Select all,
none,
unread,
read,
favourite
</p>
<p id="messages">
<label class="unread">
<input type="checkbox" name="message" value="1" class="unread" /> Hi, man!
</label><br/>
<label class="read fav">
<input type="checkbox" name="message" value="2" class="read fav" /> Cute kittens, look
</label><br/>
<label class="read">
<input type="checkbox" name="message" value="3" class="read" /> Pysh-pysh, ololo
</label><br />
<label class="unread">
<input type="checkbox" name="message" value="4" class="unread" /> New important task!
</label><br/>
</p>
CSS:
label.unread {
font-weight: bold;
}
label.fav {
background-color: #F5E942;
}
a.pseudo {
text-decoration: none;
border-bottom: 1px dashed #4998C9;
color: #4998C9;
}
a.active {
background-color: #4998C9;
color: white;
padding: 0 0.2em;
}
jQuery:
$('#links').delegate('a', 'click', function(ev) {
// reset all checkboxes
$('input:checkbox').attr('checked', false);
// get info, what is the user choice
whichMessages = $(this).attr('id');
// do our main work - select checkboxes
switch (whichMessages) {
case 'all':
$('input:checkbox').attr('checked', true);
break;
case 'read':
$('input:checkbox.read').attr('checked', true);
break;
case 'unread':
$('input:checkbox.unread').attr('checked', true);
break;
case 'fav':
$('input:checkbox.fav').attr('checked', true);
break;
};
// add some user-frendly markup
$('#links a').removeClass('active');
$(this).addClass('active');
// and standart action to prevent standart link click event
ev.preventDefault();
});
Excuse me, I am the man, who gave you fish, but did not teach you to catch fish.
Use jquery, then
$(".parent").find("input[type=checkbox]").each(function() {
$(this).checked = true;
});
Obviously this is just an example and you won't be able to simply copy paste this, but this should get you started.

Categories