I have following html form:
<form method="post" action="">
<input type="hidden" name="userid" id="user" value="<?php echo $user ?>"/>
<textarea name="comment" class="subcomment_form" id="ctextarea<?php echo $suggestid ?>"></textarea>
<input type="submit" value="Post" id="<?php echo $suggestid ?>" class="form_btn" style="margin-top: 5px"/>
</form>
following JS code:
$('.form_btn').live("click",function()
{
var ID = $(this).attr("id");
var user= $("input[name=userid]").val();
var comment= $("#ctextarea"+ID).val();
var dataString = 'comment='+ comment + '&suggestid=' + ID + 'user' + user;
if(comment=='')
{
alert("Please Enter Comment Text");
}
else
{
$.ajax({
type: "POST",
url: "action/subcomment.php",
data: dataString,
cache: false,
success: function(html){
$("#commentload"+ID).append("html");
$("#ctextarea"+ID).val('');
$("#ctextarea"+ID).focus();
}
});
}
return false;
});
and following php code for subcomment.php file:
if($_POST['comment'])
{
$comment=$_POST['comment'];
$suggestid=$_POST['suggestid'];
$user=$_POST['user'];
$sql = "INSERT INTO user_suggestions_comments (uvd_id, user_id, usc_comment) VALUES ('".$_POST['suggestid']."', '".$_POST['user']."','".$_POST['comment']."')";
mysql_query( $sql);
}
the problem I have is that value from <input type="hidden" name="userid" id="user" value="<?php echo $user ?>"/> is not passed on php file and textarea content is passed. What I need to change inside that JS code to make it work?
It looks like your dataString is not being built to what your PHP code is expecting.
Try this change.
var dataString = 'comment='+ comment + '&suggestid=' + ID + 'user' + user;
becomes
var dataString = 'comment='+ comment + '&suggestid=' + ID + '&user=' + user;
You are looking for three values from your $_POST and only passing 2
$comment=$_POST['comment'];
$suggestid=$_POST['suggestid'];
$user=$_POST['user'];
Related
Likely a duplicate in theme/name, but no previous solutions will work either because I'm missing something or there is something else going on.
The form - seemingly at random - fails (not altering data in any field, just the defaults set in html) and when it does succeed the PHP file logs only two of the values, the second one and the fourth (both pure numbers/ints). The two it doesn't echo are 1: a text field and 3: a float.
This is the result the .php picks up -
Connected
'' + '8080' + '' + '1'
Here is the .html and .js, what on earth am I missing here?
<form id="form_0" name="form_0" method="post" action="">
<input type="text" id="coinname" name="coinname" value="litecoin"><br>
<input type="number" id="coins" name="coins" value="8080"><br>
<input type="number" id="cost" name="cost" value="0.0808"><br>
<input type="number" id="show" name="show" value="1"><br>
<input type="submit" id="submit" name="submit" value="Save">
</form>
<script>
$("#form_0").submit(function() {
var selectedcoin = $("#coinname").val();
var coins = $("#coins").val();
var buyprice = $("#cost").val();
var show = $("#show").val();
$.ajax({
type: "POST",
url: "write-database.php",
data: "selectedcoin=" + selectedcoin + "&coins=" + coins + "&buyprice=" + buyprice + "&show=" + show,
success: function(data) {
alert(data);
}
});
});
</script>
and here is the .php
<?php
$servername = "localhost";
$username = "#";
$password = "#";
$dbname = "#";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
echo "Connected <br>";
}
$selectedcoin=$_POST['selectedcoin'];
$coins=$_POST['coins'];
$buyprice=$_POST['buyprice'];
$show=$_POST['show'];
$sql= mysqli_query($conn,"INSERT INTO coin_price (coin_name, coin, price, display) VALUES ('".$selectedcoin."','".$coins."','".$buyprice."','".$show."')");
echo "'$selectedcoin' + '$coins' + '$buyprice' + '$show'";
mysqli_close($con);
?>
Just use serialize method to get all data from form values, as inputs names in $_POST global variable.
<form id="form_0" name="form_0" method="post" action="">
<input type="text" id="coinname" name="coinname" value="litecoin"><br>
<input type="number" id="coins" name="coins" value="8080"><br>
<input type="number" id="cost" name="cost" val="0.0808"><br>
<input type="number" id="show" name="show" value="1"><br>
<input type="submit" id="submit" name="submit" value="Save">
</form>
<script>
$("#form_0").submit(function() {
e.preventDefault();
$.ajax({
type: "POST",
url: "write-database.php",
data: $(this).serialize(),
success: function(data) {
alert(data);
}
});
});
</script>
<?php
$selectedcoin=$_POST['coinname'];
$coins=$_POST['coins'];
$buyprice=$_POST['cost'];
$show=$_POST['show'];
$sql= mysqli_query($conn,"INSERT INTO coin_price (coin_name, coin, price, display) VALUES ('".$selectedcoin."','".$coins."','".$buyprice."','".$show."')");
echo "'$selectedcoin' + '$coins' + '$buyprice' + '$show'";
?>
I hadn't disabled the default submit behaviour that refreshes the page...
$("#form_0").submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "write-database.php",
data: $(this).serialize(),
success: function(data) {
alert(data);
}
});
});
what iam trying to do basically and i didnt succeed is getting id from link
<input type="hidden" value="<?PHP echo $_GET['id']; ?>">
retrieve the id using javascript variable add it to api.php
var id = $("#id").val();
var firstname = $("#firstname").val();
var name = $("#name").val();
var state = $("#state").val();
var dataString = 'firstname='+ firstname + '&name=' + name + '&state=' + state;
if(firstname=='' || name=='' || state=='')
{
$('.success0').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}else{
$.ajax({
type: "POST",
url: "api.php+id",
data: dataString,
After the api.php would pickup the id, but unfortunately it dosent work
exec('curl -b cookies -c cookies -X POST -d #file.json http://sand.api.xxx.com/item?id='. $_GET['id']);
<input type="hidden" value="<?PHP echo $_GET['id']; ?>">
your code is vulnerable to xss attack
use this
<input type="hidden" id="myid" value="<?PHP echo htmlentities($_GET['id']); ?>">
var id=$("#myid").attr("value");
or
var id=$("#myid").val();
few bugs I noticed in your code:
your html should look like:
<input id="el_id" type="hidden" value="<?PHP echo $_GET['id']; ?>">
your JS fixed below:
var id = $("#el_id").val();
var firstname = $("#firstname").val();
var name = $("#name").val();
var state = $("#state").val();
var dataString = 'firstname='+ firstname + '&name=' + name + '&state=' + state;
if(firstname=='' || name=='' || state=='')
{
$('.success0').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}else{
$.ajax({
type: "POST",
url: "api.php?id="+id,
data: dataString,
I'm developing a site (only for fun and learn programming with jquery)
and i'd like to know what's wrong with this :
$(window).unload(function(){
var myid = $('input#v1').attr('value'); // hidden
var playauth = $('input#v2').attr('value'); // hidden
var srvid = $('input#v3').attr('value'); // hidden
var result = 'myid='+ myid +'&auth='+ playauth +'&srvid='+ srvid;
$.ajax({
type: "GET",
data: result,
url: "closing.php",
complete: function(data) {
alert(data.responseText);
}
});
});
I'm trying to update a database table. When i close the window nothing happens.
With a previous version of this function :
window.onunload = function () {
var xhReq = new XMLHttpRequest();
var n = document.getElementById("v1").InnerHTML;
var o = document.getElementById("v2").InnerHTML;
var p = document.getElementById("v3").InnerHTML;
xhReq.open("GET", ("closing.php?myid=" + n + "&auth=" + o + "&srvid=" + p) , false);
xhReq.send(null);
var serverResponse = xhReq.responseText;
alert(serverResponse);
};
.. i saw the response alert but GET values were 'undefined'.
.... probably because the type of inputs is hidden..?
This is my form... maybe i miss something ?? I'm really new to jquery/ajax .. please help!!
<form method="get">
<input id="v1" type="hidden" name="val1" class="aget" value="<?php echo $_GET['myid']; ?>" />
<input id="v2" type="hidden" name="val2" class="bget" value="<?php echo $_GET['playauth']; ?>" />
<input id="v3" type="hidden" name="val3" class="cget" value="<?php echo $_SESSION['srvid']; ?>" />
</form>
change
var myid = $('input#v1').attr('value'); // hidden
var playauth = $('input#v2').attr('value'); // hidden
var srvid = $('input#v3').attr('value'); // hidden
to
var myid = $('input#v1').val(); // hidden
var playauth = $('input#v2').val(); // hidden
var srvid = $('input#v3').val(); // hidden
You must use
.val(); instead of .attr('value');
On form submit i want to load a div with an updated list of a mysql table. I'am sending the form variables across to a php and posting them into a mysql table. the same page displays the full table data. I want to load the data into the same div tag as the form. So it appears that the information is being loaded above the form.
My Javascript
$("#formSubmit").submit(function(){
var name = $("input#name").val();
var comment = $("input#comment").val();
var filmnumber = $("input#hidden").val();
var dataString = 'name='+ name + '&comment=' + comment + '&filmnumber=' + filmnumber;
$.ajax({
type: "POST",
url: "comment.php",
data: dataString,
success: function() {
$('#2').load('comment.php');
}
});
My form -
<div id="2">
<p>Add a Comment</p>
<form id="formSubmit" method="POST">
<div>
<input type="hidden" name="hidden" id="hidden" value="2">
<label for="name">Your Name</label>
<input type="text" name="name" id="name" />
<label for="body">Comment Body</label>
<textarea name="comment" id="comment" cols="20" rows="5"></textarea>
<input type="submit" id="comment" class="button" value="Submit" />
</form>
</div>
All it is doing is refreshing the page and not loading the information in to div 2 :S
thanks for your help
You need to prevent the form from redirecting the page using the preventDefault method on the event object:
$("#formSubmit").submit(function(e){ // add the event object as an argument to your function
e.preventDefault(); // right here
var name = $("input#name").val();
var comment = $("input#comment").val();
var filmnumber = $("input#hidden").val();
var dataString = 'name='+ name + '&comment=' + comment + '&filmnumber=' + filmnumber;
$.ajax({
type: "POST",
url: "comment.php",
data: dataString,
success: function() {
$('#2').load('comment.php');
}
});
});
add a return false to the end to stop the form from submitting. or if you want to be more elegant use the preventDefault(); method. personally for something as simple as this though i just stick with return false;
$("#formSubmit").submit(function(e){ // add the event object as an argument to your function
var name = $("input#name").val();
var comment = $("input#comment").val();
var filmnumber = $("input#hidden").val();
var dataString = 'name='+ name + '&comment=' + comment + '&filmnumber=' + filmnumber;
$.ajax({
type: "POST",
url: "comment.php",
data: dataString,
success: function() {
$('#2').load('comment.php');
}
});
return false;//right here
});
I have a simple form, then I placed some data from the table to each of the input forms value attribute. now my problem is, whenever i typed something new to the input form, to update the data, it's unable to pick up the currently typed string, am not sure how to solve this, because I think it is picking up the value echoed out instead of the currently typed string when on this update page,
here's my front-end
<fieldset id="personaldetails">
<legend>Personal Details</legend>
Resume Title: <input type="text" name="resumetitle" id="resumetitle" value="<?php echo $v['ResumeTitle']; ?>" size="50" maxlength="50" /><br />
Name: <input type="text" name="cvname" id="cvname" size="30" maxlength="30" value="<?php echo $v['Name']; ?>" /><br />
DOB: <input type="text" id="datepicker" name="dob" value="<?php $date = new DateTime($v['DOB']); echo $date->format('m/d/Y'); ?>" /><br />
Gender: <input type="radio" name="gender" id="gender-male" value="1" <?php if($v['Gender'] == 1){ echo "checked"; } ?>/> <b>Male</b> |
<input type="radio" name="gender" id="gender-female" value="0" <?php if($v['Gender'] == 0){ echo "checked"; } ?>/> <b>Female</b><br /><br />
<input type="hidden" name="cvid" id="cvid" value="<?php echo $v['ResumeID']; ?>" />
<button name="pdetails" id="pdetails">Update</button>
</fieldset><br /><br />
//here's my js
$(document).ready(function(){
var resumetitle = $('#resumetitle').val();
var cvid = $('input[type="hidden"]').val();
var name = $('#cvname').val();
var dob = $('#datepicker').val();
var gender = $('input[name="gender"]:checked').val();
$('button#pdetails').click(function(){
$.ajax({
type: "POST",
url: "classes/ajax.resumeupdate.php",
data: "resumeid="+cvid+"&resumetitle="+resumetitle+"&name="+name+"&dob="+dob+"&gender="+gender,
success: function(){
//window.location = "resumeview.php?cvid="+cvid;
},
});
});
});
//here's my php code
require 'class.resume.php';
$db = new Resume();
if(isset($_POST['resumetitle']) || isset($_POST['name']) || isset($_POST['dob']) ||
isset($_POST['gender']) || isset($_POST['cvid'])){
$result = $db->updatepdetails($_POST['resumetitle'],$_POST['name'],$_POST['dob'],$_POST['gender'],$_POST['cvid']);
if($result){
echo "success!";
} else {
echo "failed! ".$db->error;
}
}
You are only reading the values on document ready, move that code into the click event:
$(document).ready(function(){
$('button#pdetails').click(function(){
var resumetitle = $('#resumetitle').val();
var cvid = $('input[type="hidden"]').val();
var name = $('#cvname').val();
var dob = $('#datepicker').val();
var gender = $('input[name="gender"]:checked').val();
$.ajax({
type: "POST",
url: "classes/ajax.resumeupdate.php",
data: "resumeid="+cvid+"&resumetitle="+resumetitle+"&name="+name+"&dob="+dob+"&gender="+gender,
success: function(){
//window.location = "resumeview.php?cvid="+cvid;
},
});
});
});
Your javascript is resolving the form values just once (on page load), so if you enter something after the page has loaded, the variables don't change.
You can simply calculate the values inside the Ajax callback instead. But what you really should do is use jQuery's $.serialize() function, which creates a standard a=1&b=2&c=3 querystring including the (properly escaped) form data:
$(function(){
$('button#pdetails').click(function(){
$.ajax({
type: "POST",
url: "classes/ajax.resumeupdate.php",
data: $('form').serialize(),
success: function(){
//window.location = "resumeview.php?cvid="+cvid;
}
});
});
});
Also note that you had a trailing comma after the success function - this will fail in IE so I've changed that as well.