jQuery ajax error when data fetching - php

I am using bootstrap 3.2.0 and jQuery. I can't get fetch data via jQuery and PhP
function postolayi(){
$.ajax({
type:'POST',
url:'giriskontrol.php',
data:$('#girisForm').serialize(),
success:function(cevap){
$("#uyari").html(cevap)
}
})
}
index.php
<form class="form-signin" method="POST" id="girisForm">
<input id="isim" name="isim" type="text" class="form-control" placeholder="isim" required autofocus>
<input name="sifre" type="password" class="form-control" placeholder="şifre" id="sifre" required>
<button class="btn btn-lg btn-primary btn-block" type="submit" onclick="postolayi();">
Giriş Yap</button>
</form>
<div class="alert alert-warning" id="uyari"></div>
giriskontrol.php
<?php
$POST["isim"] = $isim;
echo $isim;
?>
I can't fetch data. Please somebody help.
I changed as
<?php
$isim = $POST["isim"];
echo $isim;
?>
But still not working.

You are overwriting the post variable here:
$_POST["isim"] = $isim;
What you probably wanted was:
$isim = $_POST["isim"];
echo $isim;

In your php file
Switch:
$POST["isim"] = $isim;
with
$isim = $POST["isim"];

You need to switch your assignment on your php page to
<?php
$isim = $_POST["isim"];
echo $isim;
?>

Related

isset($_POST['Submit'] not set after submit

I know it seems to be a common problem but I searched a lot of them and nothing helped me. I have the following code and on Submit, nothing happens aside from a page reload. I tried with enctype and without, with <input type=button ...> and <button ...> but I never got the echo...
<?php
if (isset($_POST['Submit']) && !empty($_POST['Submit'])){
echo 'It works';
} else {
?>
<form method="post" enctype="multipart/form-data" action="">
<div class="form-group">
<label for="name" class="control-label">Name</label>
<input name="name" type="text" class="form-control" id="name" placeholder="Name"
aria-describedby="helpBlock"
oninvalid="this.setCustomValidity('Bitte tragen Sie Ihren Namen ein.')"
oninput="setCustomValidity('')"
required>
</div>
<!-- multiple other form-groups like the first one -->
<div class="form-group">
<input name="Submit" type="submit" class="btn btn-default" value="Abschicken">
</div>
<div id="helpBlock">* Alle Felder sind Pflichtfelder.</div>
</form>
<?php } ?>
UPDATE:
I run this code on localhost via xampp
deleting !empty($_POST['Submit']) doesn't help either
yes, it is a .php file
UPDATE:
And also if I try entering
foreach ($_POST as $key => $value) {
echo 'index key ' . $key . ' value ' . $value . '<br>';}
nothing shows up on the page - so I assume that the $_POST variable doesn't get set... can anybody tell why?
I solved the case: the problem was, that because of the URL localhost[...]/test.php?nav=kontakt the form always tried to execute a GET request. Now I have to use a static page instead of the test.php with parameter

Refresh page after alert box using AJAX in php

I'm doing a form validation in my php script, take one column as an example, if the username is not filled, an alert window will pop up and say "please enter your username", after user click "ok", the whole page refresh but the information on the form will be reset too.
So I would like to keep what the user has input after refreshing the page, how can I embed the code in php using AJAX?
//username validation
if (empty($username)) {
$error = true;
echo '<script language="javascript">';
echo 'alert("Please enter your username")';
echo '</script>';
//refresh the page
header("Refresh:0; url=register.php");
the website is in php file, html code is embedded under the php stuff, and this is the form in the html
<div id="account">
<form method="POST">
<p><span class="error">* required field.</span></p>
Username:
<input type="text" name="Username">
<span class="error">* </span><br>
<!--other fields..-->
<input type="reset" value="Reset">
<input type="submit" value="Submit" name="signup_button">
</form>
</div>
<?php
if(isset($_POST['signup_button'])){
$Username = $_POST['Username'];
}
?>
or...
if (empty($username)) {
$error = true;
echo '<script language="javascript">';
/* save the value in the browser */
echo 'window.'+VarYouWantToKeep = $_POST['VarYouWantToKeep'];
echo 'alert("Please enter your username")';
echo '</script>';
//refresh the page
header("Refresh:0; url=register.php");
( or use localStorage, instead of window. https://developer.mozilla.org/en/docs/Web/API/Window/localStorage )
Then, when the page loads, check if('window.'+VarYouWantToKeep)
and if it's there, set it as the value="" of the corresponding form field
try this (This is taking into account you set $username to $_POST["Username"];)
<input type="text" name="Username" value="<?php if(isset($_POST["signup_button"]) && $_POST["signup_button"]=="Submit") {echo $username; ?>"
To store variable data, you will need to use $_SESSION variables in PHP. Make sure that session_start() is at the top of the PHP page.
Here's a basic example:
//this PHP page contains the HTML form
<form>
<input type="text" value="<?php echo $_SESSION['email']; ?>" />
</form>
//ajax
$.ajax({
url:'validate.php',
type:'POST',
data:{name:inputName}
}).done(function(data){
alert(data);
});
//php page
session_start();
if(!isset($_POST['name'])){
//set session variable
$_SESSION['email'] = $_POST['email'];
//this will be sent back to PHP page with HTML
echo 'Please enter username';
}
I coded here, a super cool login page with bootstrap as you expect. Link bootstarp cdn in your html file. This will show the error under the button instead of showing a windows classic alert box. I included ajax request and php response also with some validation.
login.html
<div class="container">
<div class="col-lg-offset-8 col-md-offset-6 col-lg-4 col-md-4 login-bg">
<form class="form-horizontal" role="form" action="#" method="">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input id="username" type="text" class="form-control" name="username" value="" placeholder="User Name" required tabindex="1" autocomplete="off">
</div>
<br>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
<input id="password" type="password" class="form-control" name="password" value="" placeholder="Password" required tabindex="2" autocomplete="off">
</div>
<br>
<div class="form-group">
<div class="col-lg-6">
<button class="btn btn-primary-outline btn-block" type="submit" id="login">Login</button>
</div>
<div class="col-lg-6">
<button class="btn btn-success-outline btn-block" type="reset">Clear</button>
</div>
</div>
<br>
<div id="login-feedback" class="btn-block"></div>
</form>
</div>
</div>
myStyle.js
<script type="text/javascript">
$(document).ready(function()
{
$('#login').click(function()
{
var uname=$('#username').val();
var pword=$('#password').val();
if(uname!='' && pword!='')
{
$('#login-feedback').text('validating....');
$.post("login-check.php",{username:uname,password:pword},function(data)
{
if(data=="success")
{
$('#login-feedback').fadeTo(200,0.1,function()
{
$(this).html(data).css('color','green').fadeTo(1000,1,function()
{
document.location='index.php';
});
});
}
else
{
$('#login-feedback').fadeTo(200,0.1,function()
{
$(this).html(data).css('color','red').fadeTo(900,1);
//reset form
$('#login').trigger("reset");
});
}
});
return false;
}
else
{
$('#login-feedback').text("Please enter all fields").css('color','purple');
}
});
});
</script>
login-check.php
<?php
//include connextion file;
if(!isset($_SESSION))
session_start();
$username=trim($_POST['username']);
$password=$_POST['password'];
$username=mysqli_real_escape_string($con,$username);
$password=mysqli_real_escape_string($con,md5($password));
if(($username) && ($password))
{
$query = mysqli_query($con,"select * from login where password='$password' AND username='$username'");
$rows = mysqli_num_rows($query);
if ($rows>= 1)
{
$fetch_result = mysqli_fetch_array($query);
session_regenerate_id(true);
$_SESSION['username']=$fetch_result['username'];
$_SESSION['status']=$fetch_result['status'];
// Close session variable assigns
session_write_close();
echo "success";
}
else
{
echo 'Login failed';
}
mysqli_close($con);
}
?>

How to remove button after submit form using PHP

I need one help.I need to totally remove the submit button after submit the form and when it will be submitted the button will display to user.I am explaining my code below.
<form name="billdata" id="billdata" enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px"> Name :</span>
<input type="text" name="u_name" id="name" class="form-control" placeholder="Add Name" onKeyPress="clearField('name');">
</div>
<input type="submit" class="btn btn-success" name="complainSubmit" id="addProfileData" value="Submit"/>
</form>
complain.php:
require_once("./include/dbconfig.php");
if(isset($_REQUEST['complainSubmit']))
{
// ......data is collecting here....
}
when data are submitted successfully the below part is executing.
<script type="text/javascript">
var phpVar = "<?php echo $_GET['success'];?>";
//console.log('php',phpVar=='');
if(phpVar == 1 && phpVar!=''){
alert('Submitted successfully.');
//var subButton=document.getElementById('addProfileData');
//subButton.disabled=false;
}
else if(phpVar == 0 && phpVar!=''){
alert('Unable to add.\\nTry again.');
}
else{
// nothing
}
</script>
<script>
function checkForm(){
var s=document.billdata;
if(s.u_name.value==''){
alert('Please enter name');
s.u_name.focus();
s.u_name.style.borderColor = "red";
return false;
}
}
</script>
Here i need to button hide when the data is going to submit and it will again display after the submit.Please help me.
A javascript code such as:
document.getElementById("your_div").innerHTML = "";
will erase the content of your div.
So, byt tagging the entire form, then erasing its contents, you can "hide" it.
Why you dont add a hide CSS-Selector or remove it by checking via if-statement?
CSS-Version:
<input type="submit" class="btn btn-success <?php ($isSubmitted ? ' hideme' : '')?>" name="complainSubmit" id="addProfileData" value="Submit">
Except a new CSS-Selector: .hideme { display:none }
Via PHP/Template:
html...
<?php if(!$isSubmitted) : ?>
<input type="submit" class="btn btn-success" name="complainSubmit" id="addProfileData" value="Submit">
<?php endif; ?>
html...
Dont forget: You dont need close input html-tag: <input/> just <input>
And what is wrong to hide the button via JS?
document.getElementById("addProfileData").style.display = "none";
UPDATE:
Prompt hiding after clicking:
<button onclick="javascript:this.style.display='none'">
Submit Button
</button>
in your example:
<input onclick="javascript:this.style.display='none'" type="submit" class="btn btn-success" name="complainSubmit" id="addProfileData" value="Submit"/>

How to Call PHP Function in a Class from HTML Button Click

I am a rookie PHP developer.
I have a PHP web project with an HTML page that contains an Add button. The name of the page is Awards.html. Elsewhere I have created a PHP class, Awards.php which contains a function.
The source code of my files is given as follows:
Awards.html
<div class="divparent">
<div class="modal-header">
<div class="btn-group">
<button class="btn" data-bind="click: closeModal">Exit</button>
</div>
</div>
<div class="modal-title">
<h1 id="headerid">Awards</h1>
</div>
<div class="modal-body">
<input id="hdnValueCurrentAwardSoid" type="hidden" value="null" />
<div class="divleft">
<input id="txtName" maxlength="80" type="text" class="water newpost1" placeholder="Award Name" tabindex="1" />
<section class="ThumbnailContainer">
<img id="imgThumbnail" class="imgThumbnail" />
<img src="http://localhost/rockontechnologies/Images/GreenRibbon.png" id="pictureribbon" class="pictureribbon" />
<input type="text" contenteditable="false" readonly id="transformtext" />
</section>
<textarea id="txtdescription" placeholder="Description" class="water newpost1" rows="4" tabindex="2"></textarea>
<div class="ui-widget">
<input id="txtIssueOrg" maxlength="50" type="text" placeholder="Issue Organization" />
</div>
</div>
</div>
<div class = "divbottom">
<div id="divAddAward">
<button class="btn" onclick="">Add</button>
</div>
</div>
</div>
Awards.php
<?php
class Awards
{
function clickFunction()
{
//Function that needs to be executed!
}
}
The problem here is that on the click event of the Add button, I need to call the clickFunction() of the Awards.php file. Can anyone please tell me how to do this?
Replies at the earliest will be highly appreciated. Thank you.
You should do something like that
<button class="btn" onclick="onrequest();">Add</button>
function onrequest() {
$.post(
'example.php'
).success(function(resp){
json = $.parseJSON(resp);
alert(json);
});
}
and in example.php call your function
$class = new Awards();
$method = $class->clickFunction();
echo json_encode($method);
and in your clickFunction();
clickFunction(){
$array = array(
'status' => '1'
);
return $array;
}
first of you have to a instance of class which is like that
$class = new Awards();
then if you want to onclick event you should make a javascript code but how to get the function you can do like this
<?php echo $class->clickFunction(); ?>
I think you can't call a class directly from HTML in PHP you have to call it through HTTP by loading a page.
Hence, use a AJAX call to call Awards.php. Eg. with JQuery it could look like this $.get("Awards.php", function() {
alert( "success" );
})
In your php-file you should also call your class something like this <?php echo $class->clickFunction(); ?>

insert form to database

i have program codeigniter anda i want to insert form to database.
code view:
<form target="paypal" method="post">
<div class="field1">
<div class="field">
<label>Nama</label>
<input placeholder="Nama" name="nama" type="text">
</div>
<div class="field">
<label>No. HP</label>
<input placeholder="No. HP" name="handphone" type="text">
</div>
<div class="field">
<label>Alamat</label>
<input placeholder="alamat" name="alamat" type="text">
</div>
<div class="field">
<label>Jumlah</label>
<div class="selectbox">
<select name="jumlah" id="">
<?php for ($i=1; $i <= 20; $i++): ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>
</div>
</div>
<button type="submit" name="submit" class="ui teal button order-button">Order now</button>
</div>
</form>
code controller
function simpanOrder()
{
$this->load->model("M_order");
$data['nama'] = $_POST['nama'];
$data['handphone'] = $_POST['handphone'];
$data['alamat'] = $_POST['alamat'];
$data['jumlah'] = $_POST['jumlah'];
if($this->input->post('submit')){
$this->M_order->insert($data);
}
}
when i click submit data not insert to database. so can you help me with this code problem? thanks.
Your form doesn't have an action, and therefore may not be going to the function you want it to. (/controller/function)
<form target="paypal" method="post">
Also, instead of using a button to submit the form - try using <input type="submit"...
Using the <button>, in some browsers, you would have "submit" submitted, in others, "Order now".
If the above doesn't work - check your SQL.
As a side note, CodeIgniter has a form helper and a form_validation library which are quite useful if you're already using CodeIgniter. That won't fix your problem but it's just something I felt I would point out.
See:
http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html
http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
Call model from controller. and write below code in model.
$data = array(
'handphone' => $this->input->post('handphone'),
'alamat' => $this->input->post('alamat'),
)
In this array key is database columnname.
$this->db->insert(yourtablname, $data);
$insert_id = $this->db->insert_id();
You need to define action attribute in form tag where you will provide controller name and method name like this
<form action="<?php echo site_url('controllername/simpanOrder')?>" method="post">
After posting you can debug your code like this
$post = $this->input->post();
echo '<pre>';
print_r($post);
Then
if($this->input->post('submit')){
$this->M_order->insert($data);
}
And finally
echo $this->db->last_query();
This will display you the last query run.

Categories