First form is where client can get all products by Make Model and Price as radio button with value of there id in database
Client click submit.
PHP ask isset name of submit button
If isset it echos second form where client can add specifications for product that he or she choose. Form is input type text and textarea's
Then php ask isset name of submit button for second form
but the problem is when client press submit for second form
If first form is unset then the rest of code should check that second form don't exist because both forms most exit for code to work and I can get that, if I don't find away to stop page reload on submit
I try using AJAX but the MySQL query throws an error that inputs are empty.
JavaScript code:
<script>
$( document ).ready(function() {
$('.link').click(function(){
$.post('ajax1.php?strana='+$(this).attr('strana'), function(odgovor) {
$('#odgovor').html(odgovor);
});
});
});
</script>
PHP code:
echo "<div class='container' id='cars'>
<div class='row'>
<div class='col-md-4 col-md-offset-4 text-center'>
<form action='' method='post' class='loginForm' id='cmsC' name='cmsC' enctype='multipart/form-data'>
<div class='input-group'>";
$sql="select * from produkt ";
$rez=mysqli_query($db,$sql);
while($red = mysqli_fetch_object($rez))
{
echo "<input type='radio' name='proizvod' value='$red->id'> ". $red->marka." ". $red->naziv." ". $red->cena."<br>";
}
echo "<input type='submit' id='btnsubmit1' name='btnsubmit1' class='form-control' value='Chouse Product' >
</div>
</form>
</div>
</div>
</div>";
if (isset($_POST['btnsubmit1'])) {
$id = $_POST['proizvod'];
echo "<div class='container'>
<div class='row'>
<div class='col-md-4 col-md-offset-4 text-center'>
<form action='' method='post' class='loginForm' id='cms' name='cms' enctype='multipart/form-data'>
<div class='input-group clear'>
<input type='text' id='dotm' name='dotm' class='form-control'
placeholder='Description Of The Motherboard ( ASUS H81M-R/C/SI )'>
<input type='text' id='top' name='top' class='form-control'
placeholder='Type Of Processor ( Intel® Core™ i3 Processor )'>"; // there is bunch more of this type for input type
if (isset($_POST['submit'])) {
// Variable with data from form
$description_of_the_motherboard = $_POST['dotm'];
$type_of_processor = $_POST['top'];
$processor_description = $_POST['pd'];
$type_of_graphics_card = $_POST['togc'];
if(strlen($description_of_the_motherboard)!="") {
if(!preg_match("/[^\w\s,.\\\'\"\-\/]/", $description_of_the_motherboard) {
$sql = "insert query";
mysqli_query($db, $sql);
} else echo "<script>
alert('Your input can not have special characters ');
</script>";
}
}
}
Just add a flag to avoid duplicate requests.
Example:
$( document ).ready(function() {
var iAmProcessing = false;
$('.link').click(function(){
if (iAmProcessing === true) {
return;
}
iAmProcessing = true;
$.post('ajax1.php?strana='+$(this).attr('strana'), function(odgovor) {
iAmProcessing = false;
$('#odgovor').html(odgovor);
});
});
});
data post most be into the array in second parameter.
<script>
$( document ).ready(function() {
$('.link').click(function(){
$.post('ajax1.php',{strana:$(this).attr('strana'),function(odgovor){
$('#odgovor').html(odgovor);
});
});
});
</script>
Related
i have a form with four elements. i need to open a jquery popup when click on image that i set as fourth element in my form. popup window contains another form and a submit button. herepopup not coming. what wil i do.
this is my form
echo "<div class=\"addform\">
<form method='GET' action=\"update_events.php\">\n";
echo " <input type=\"hidden\" name=\"column1\" value=\"".$row['event_id']."\"/>\n";
echo " <input type=\"text\" name=\"column2\" value=\"".$row['event_name']."\"/>\n";
echo " <input type=\"text\" name=\"column3\" value=\"".$row['description']."\"/>\n";
echo " <input type=\"image\" src=\"images/update.png\" id=\"update_event\" alt=\"Update Row\" class=\"topopup\" onClick=\"callPopup(".$row['event_id'].")\"; title=\"Update Row\">\n";
}
echo "</table></form><br />\n";
this is my jquery
<script type="text/javascript">
function callPopup(id) {
console.log(id);
var datastring = "&event_id="+id;
$.ajax({
url: 'event_edit_popup.php', //enter needed url here
data: datastring,
type: 'get', //here u can set type as get or post
success: function(data) {
$('.popupContent').html(data);
console.log(data);
$('.loader1').hide();
$("#popup_content").after(data);
// u can see returned data in console log.
// here, after ajax call,u can show popup.
}
});
};
</script>
and this is my popup div
<div id="toPopup">
<div class="close"></div>
<span class="ecs_tooltip">Press Esc to close <span class="arrow"></span></span>
<div id="popup_content"> <!--your content start-->
<p align="center">edit company</p>
</div> <!--your content end-->
</div> <!--toPopup end-->
<div class="loader"></div>
<div id="backgroundPopup"></div>
You just need to give the image an id.. say id="clickme"
and in the jquery script:-
$('document').ready(function(){
$('#clickme').click(function(){ $('#topopup').show(220);}); });
Again u can add in transitions in the css of the topopup to give it various effects.
Also to hide the pop up:-
$('document').ready(function(){
$('#backgroundPopup').click(function(){ $('#topopup,#backgroundPopup').hide(220);}); });
//This is assuming that you want the popup to be closed when u click on the background
First mistake in your form is not complete and second is there is no input type='image' if you want to display image than you use image tag.
Please follow the code I hope it will be helpful to you:
<div class='addform'>
<form method='GET' action='update_events.php'>
<input type='hidden' name='column1' value="123"/>
<input type='text' name='column2' value="456"/>
<input type='text' name='column3' value="789"/>
<img src='images/update.png' id='update_event' alt='Update Row' class='topopup' onClick='callPopup("1")' title='Update Row'/>
</form>
</div>
Now jQuery code:
$("#update_event").click(function() { alert('sdf'); });
Now instead of alert you can use your ajax call for pop up.
Im trying to use PHP to delete a row in a SQL database.
The row is decided by the name of the product the user types in.
I created a simple form to delete products that have already been entered:
<article>
<section>
<fieldset><legend><span>Would you like to add a Product?</span> </legend>
<form method="POST" id = "myForm" name="myForm" onsubmit="return false;">
<br>
<p>Enter a name of product <input type='text' id='name' name = 'name'/></p>
<br>
<input name="submit" id ="submit" type="button" value="Find Product"/>
</form>
</fieldset>
<div id="fetchProduct">
</div>
</section>
</article>
Say I have already entered the data on another form and it goes into the database, but when I try to delete it I am getting the Undefined index: name in error when i try to delete.
Here is the script im using to delete the product:
<?php
include("database/connect_database.php");
$name = $_POST['name'];
$query = "DELETE FROM products WHERE product_name = '$name' ";
$result = $database->query($query) OR die ("Failed query $query");
echo $database->error."<p>";
if($result){
echo "Record deleted";
}
else {
echo "Product not found!";
}
?>
Im also using AJAX to pass through the name:
fetch = function () {
// declare the two variables that will be used
var xhr, target, changeListener;
// find the element that should be updated
target = document.getElementById("fetchProduct");
var name = document.getElementById("name").value;
var variable = "name="+name;
// create a request object
xhr = new XMLHttpRequest();
changeListener = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
target.innerHTML = xhr.responseText;
} else {
target.innerHTML = "<p>Something went wrong.</p>";
}
};
xhr.open("POST", "deleteProductSQL.php", true);
xhr.onreadystatechange = changeListener;
xhr.send(variable);
};
pageLoaded = function () {
var fetchButton = document.getElementById("submit");
if (fetchButton) {
fetchButton.addEventListener("click", fetch);
}
};
window.onload = pageLoaded;
Could anyone point out where im going horribly wrong here because I just cant identify why it isn't allowing me to delete the row and why im receiving the undefined error.
Thankyou for your time
Put your PHP code in isset()
if (isset($_POST['name']){
//code here
}
Also
<p>Enter a name of product <input type='text' id='name' name = 'name'/></p>
In this line, <p> is not a self-closing tag so
<p>Enter a name of product <input type='text' id='name' name = 'name'></p>
I'm trying to prevent the default action when submitting a form with ajax but I believe I have my code wrong as the page seems to do a 'refresh' and the form clears but nothing is sent to the database. I tried adding the link to the php processing script in the 'action' part of the form and it does submit fine to the database so the problem seems to be with my jQuery code.
<script type="text/javascript">
$(document).ready(function(e){
e.preventDefault();
$("#rpack_add_form").validate({
submitHandler: function(form) {
// do other stuff for a valid form
$.post('<?php echo BASE_URL?>/core/addrpack.php.php', $("#rpack_add_form").serialize(), function(data) {
$('#ajaxResult').html(data);
});
}
});
});
</script>
My form code:
<div id="rpacks_admin" style="display: none;">
<h5>Add A New Recovery Pack</h5>
<form id="rpack_add_form" class='small_form' name='rpack_add_form' action='' method='post'>
Contract:
<select id="contract_select" name="contract" onchange="showContract(this)">
<option value='0'>Select Contract</option>
<?php
$sth = $conn->query("SELECT * FROM `contracts`");
while($row = $sth->fetch(PDO::FETCH_ASSOC))
{
echo '<option value='.$row['contracts_id'].'>'.$row['contracts_name'].'</option>';
}
?>
</select>
<div id="contract1" class="admin_box">
Prefix: <input name='prefix' type='text' id='prefix'><br />
Number: <input name='number' type='text' id='number'><br />
Suffix: <input name='suffix' type='text' id='suffix'><br />
</div>
<div id="contract2" class="admin_box">
<p>Sapce for content</p>
</div>
<div id="contract3" class="admin_box">
<p>Sapce for contentrm</p>
</div>
Received:
<select id="select_receive" name="received" onchange="showLocation(this)">
<option value="0">No</option>
<option value="1">Yes</option>
</select><br />
<div id="location_box" style="display: none; padding-top: 5px;">Location: <input name='location' type='text' id='location'></div>
<input class='button' type=submit value='Add' name='add_rpack'>
</form>
<a class='hide_div' href='javascript:void(0);' onclick='hideRdiscDiv()'>Close</a>
and my PHP if needed
<?php
session_start();
include_once 'config.php';
include_once 'connect.php';
include_once 'bcrypt.php';
$prefix = $_POST['prefix'];
$number = $_POST['number'];
$suffix = $_POST['suffix'];
$contract = $_POST['contract'];
$received = $_POST['received'];
$location = $_POST['location'];
//Check if password and username has been submitted then add to DB
if (empty ($number))
{
echo "You need to enter a recovery pack number";
}else
{
$sth = "INSERT INTO `rpacks` (rpacks_prefix, rpacks_number, rpacks_suffix, rpacks_contract, rpacks_receive, rpacks_location) VALUES (:prefix, :number, :suffix, :contract, :received, :location)";
$q = $conn->prepare($sth);
$q->execute(array(':prefix'=>$prefix,':number'=>$number,':suffix'=>$suffix,':contract'=>$contract, ':received'=>$received, ':location'=>$location));
echo "Added";
}
The .validate() object has a sendForm parameter. It is set to true by default, but you need to set it to false, as in the code below, to prevent the form from being submitted:
$(document).ready(function () {
$('#rpack_add_form').validate({
submitHandler: function (form) {
// do other stuff for a valid form
$.post('<?php echo BASE_URL?>/core/addrpack.php.php', $('#rpack_add_form').serialize(), function (data) {
$('#ajaxResult').html(data);
});
},
sendForm: false
});
});
You can reference the docs for more info.
I add input text box dynamically and trying to pass values using POST method but everytime I submit the form, I only receive the first value.
<script type="text/javascript">
function add_ele() {
// For Internet Explorer
try {
el = document.createElement("<input type='text' name='cat[]' >");
}
// For other browsers
catch (e) {
el = document.createElement('input');
el.setAttribute('type', 'text');
el.setAttribute('name', 'cat[]');
}
document.getElementById('cate').appendChild(el);
}
</script>
<form action='submit.php' method='POST' enctype='multipart/form-data'>
<div id='cate' style='width:100px'>
<input type='text' name='cat[]' value='kjkj'>
</div>
</form>
Add Another
Submit page:
$p_cat = $_POST['cat'];
foreach ($p_cat as $p_cate ) {
echo "$p_cate . <br>";
}
Someone please guide.
If that is your complete code, you're not setting the <input>'s value attribute, so there's nothing to submit. Here's a modified (and cleaned up) example:
<script type="text/javascript">
function addElement() {
var input = document.createElement('input');
input.setAttribute('type', 'text');
input.setAttribute('name', 'cat[]');
input.setAttribute('value', 'some value');
document.getElementById('cate').appendChild(input);
// We need to return false so the href isn't followed.
return false;
}
</script>
<form action="submit.php" method='POST' enctype="multipart/form-data">
<div id="cate" style="width: 100px">
<input type="text" name="cat[]" value="kjkj">
</div>
</form>
Add Another
You should use double-quotes in HTML markup ( not apostrophes ). This should work :
el = document.createElement('<input type="text" name="cat[]" >');
I guess you have to use double-quotes in html markup. This should work :
i have the following jquery code, which is link to a html form which is link to a php script. the form should submit and the jquery code should update the comment and show the comment as if the page was referesh. but nothing happens when i hit submit i cant seem to see where or what i am doing wrong.
<script type="text/javascript">
$(function() {
//When submit button clicked
$('#submit').click(function(){
//Getting data from input fields
var sentby_val = $('#sent_by').val();
var message_val = $('#message').val();
$.post('comment.php', { username: sentby_val, message: message_val }, function(return_data){
//Response from script when comment inserted to database
alert(return_data);
//Clean fields
$('#sent_by').val('<?php print $_SESSION['email']?>');
$('#message').val('');
});
});
});
</script>
here is the html form
<form action='comment.php' method='post' style="width: 422px">
<input type='hidden' id="sent_by" name='sent_by' value="<?php print $_SESSION['email']?>"/>
<input type='hidden' id="hidden_id" name='hidden_id' value='<?php print $picid;?>'/>
<textarea name='message' id="message" value="make your comment" style="width: 450px; height: 70px">make your comment</textarea><br/>
<input type='submit' name='sub' value='comment' id="submit" style="border-style:none; float:right;" />
</form>
here is the comment.php // which shouldnt really be the problem
<?
$name = $_POST['sent_by'];
$picid= $_POST['hidden_id'];
$message = $_POST['message'];
$sub = $_POST['sub'];
if ($sub){if($name&&$message&&$picid)
{
$insert = mysql_query("INSERT INTO comment (name,message,picid) VALUES ('$name','$message','$picid')" );
}
else
{
echo "Please enter a comment";
}
header("location:../profile.php?pic=$id");
?>
please help what am i missing or not seeing
Try this:
<script type="text/javascript">
$(function() {
//When submit button clicked
$('#submit').click(function(){
//Getting data from input fields
var sentby_val = $('#sent_by').val();
var message_val = $('#message').val();
$.post('comment.php', { username: sentby_val, message: message_val }, function(return_data){
//Response from script when comment inserted to database
alert(return_data);
//Clean fields
$('#sent_by').val('<?php print $_SESSION['email']?>');
$('#message').val('');
});
// Overrides default click handler.
// Without this the form will be submitted
return false;
});
});
</script>