I am having difficulty making a button that removes existing values in MySQL database field. I have read up some information on it, some say use AJAX, but i'm not sure. What are your opinions? Can any Stackoverflow-er show me a method? I recognise I cannot use Javascript as it is on the client side and not server-side - but is there another way?
create form first in html
<form method="get" action="delete.php">
Enter id you want to delete : <input type="text" name="id">
<input type="submit">
</form>
delete.php
<?php
$dd=$_GET["id"];
$c=mysql_connect("localhost","root","");
mysql_select_db("mydatabase")
mysql_query("delete form mytable where id=$dd");
print "Remove successfully...";
?>
This upper code will remove the entries you want on clicking of submit button
If you want to remove the values from mysql database asynchronously then you'll need to use AJAX and that's the only option else if you are allowed to do it without refreshing the page then go with simple PHP.
you need not to do it with ajax if u dont want. simple php is good enuf..
just make a form with a button field.
post a variable though it to your php.
connect with your database using update the dtabase as you want..
Beware : One refresh would take place on this process as its not an asynchronous process. else you can choose AJAX
you could use a (post/get) request to a file on your server but i recommend you use AJAX to do this, the server side file will still look pretty much the same.
if you are using php for the back end the code would look like
$mysqli = new mysqli("localhost", "user", "password", "database");
$res = $mysqli->query('your delete statement');
if(! $mysqli->errorno )
header('Location:success url');
else
header('Location:fail url?error=' . $mysqli->error')
you could use a link for you button
<a class="button" href="script to delete">delete unwanted data</a>
Related
I have this submit form of PHP. Then when i submit it --it goes to my database.
It does save, but i need to refresh my page to see the data that has been save to my DB.
My Page -> Submit DB -> My Page
Sort of like that, so i in the save page when i do the submit function.
Why do i need to refresh my page to see the result of the data that has been saved?
Or do i need to create a new page? or do i need to add some queries on url?
Thank you.
UPDATED:
Thank you for all the help guys.
I just used the isset, and its working now
Probably you're querying data first and then you're saving form to database. Check it and change sequence if it is true.
Assuming your Submit DB code is on same My Page.
At the start of page check if submit button is clicked
<?php
if(isset($_POST['submit']))
{
//your insert query
}
?>
In your body tag
<?php
// display your table
?>
You should use a single page both displaying, inserting and reading from/into database
<?php
if (isset($_POST['insert']) && $_POST['insert'] == "yes")
{
//run your insert query
}
?>
<!-- put your form here -->
remember to add the code below into your form
<input type="hidden" name="insert" value="yes" />
<!-- put your table here -->
I am trying to submit a form using a hyperlink and it is not posting values onto the next page.Here is my code form:
<?php
$email = array('name'=>'accountno','id'=>'accountno','value'=>set_value('email'));
?>
<form method="post" id = "login_form" action="/salesrep/check" name = "login_form" class="custLogin">
<fieldset style="color: #BD1313; width: 440px;"> <input type="hidden" name="submit_type" id="submit_type" value="account_only">
<br><center><label for="customerNo"><b>Customer No:</b></label>
<? echo form_input($email);?>
Submit<? echo form_input($button);?> </center>
<p> </p>
</fieldset>
</form>
The code on the next page looks like this:
<?
print_array($_POST);
die();
?>
When i use the button here,it posts values to next page successfully.BUT I HAVE not been able to post values using the hyperlink on onclick event. Where i am making mistake??
Why i am getting empty array when i am already inserting value in the text box.?? Or is there any way i could post values using the link and retrieve them in the next page???
The real problem is that you are trying to use a link plus some JavaScript to submit a form in the first place. Don't do this. Buttons inform users that they will submit the form. They will show up in screen readers when they are in forms mode (a link with some JavaScript won't). They "just work".
If you insist on using a link and some JavaScript, then the reason that your code doesn't work is that the JavaScript runs, the form starts to submit, then the link is followed and a GET request is made to the page instead.
Normally you could call preventDefault on the event to stop this, but you are using old style intrinsic event attributes so you need to return false; from there instead.
Recommended reading: Progressive Enhancement and Unobtrusive JavaScript
hi i am new to all html javascript and php. this is suppose to save the input from a html form to my database... but i have no idea why this doesnt work... the code dosent seem to go into the php section to execute the command...
kindly advise thank you
<html>
<script type = "text/javascript">
function processInputs()
{
//get data from html form
var email_data;
email_data=document.getElementById('txtInput').value;
//document.write(email_data);
<?php
//connect database
$con = mysql_connect("localhost","admin","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("internet", $con);
$newemail = $_GET['email_data'];
echo $newemail;
//query
mysql_query("INSERT INTO user_data (email) VALUES ($newemail)");
mysql_close($con);
?>
}
</script>
<FORM action="test.php" method="post">
<INPUT type="text" value="this is a text" name"txtInput" id="txtInput" />
</FORM>
<button type="button" onclick=processInputs() >click!</button>
You have multiple flaws:
you cannot "embed" like that a PHP script into a javascript function, for that you should use AJAX - try jQuery. PHP is server side, Javascript is browser based.
You have a MySQL injection hole there, you should escape your $_GET with mysql_real_escape_string()
Stop using mysql_* as those functions are deprecated. Read this article
onclick is invalid, it should be <button type="button" onclick="processInputs()">click!</button>
If you have a perception that -
On calling the js function on some user actions such as button click/hover, etc would trigger the php code and populate the data,
then YOU ARE WRONG.
PHP is a server side scripting language, and you can't use it this way. The complete page is parsed at the server end and given as response to the browser. To do dynamic updates use Ajax calls that would call a php page and populate the html for you.
I suggest refer the coredogs.com site for better understanding of php flow.
your function is not being called !!!
Change this
onClick = processInputs()
to
onClick = "processInputs()"
and try for an alert statement to see if function is being called or not
Also you cannot have php script within script tag
Im trying to create a grid on my page in each cell there will be a simple one line form. If a person enters data into lets say FieldA I would like the php to perform actionA but if the data was entered in FieldF I would like actionF performed. Is this possible without having to create a php for each cell and upload all those php files?
Or is there a way to perform the GET method in each form to append the data to the end of the action url without the field name showing (ie sample.com/somestuff/fieldA instead of sample.com/somestuff/fieldname=fieldA) thus not needing php at all?
Did you try anything. Please try to write some code. If you get struck paste the code here, somebody will help you out..
In my opinion, why you need different forms. Just have a form which has n text boxes and perform the task that you need.
Your problem is a little ambiguous to me. I'll give it a shot though.
On the form I would set:
`method="post" action="<?php echo $PHP_SELF ?>"`
This will cause the form to submit back to itself. Then at the top of the page you could do something like the following:
<?php
if (isset($_POST["fieldA"]){
performActionA();
} else if (isset($_POST["fieldB"]){
performActionB();
}
etc...
?>
Is that what you are trying to do? Keep in mind php is executed server side before any interaction with the user.
otherwise you could use javascript to change the action field of the form. (Untested)
<script type="text/javascript">
function setAction(elt){
var page = document.getElementById(elt).value;
document.myform.action="sample.com/somestuff/"+page;
}
</script>
<form id="myform" action="sample.com/somestuff/">
<input type="text" name="text1" onchange="setAction('text1')" />
</form>
I used the code below to remove a selected item from drop down, but when I remove one, the other item pops up. For example, if these are my options: "guns, cars, money", as I select and delete guns, cars and money remains. However, if I select cars and delete it, the deleted guns options pops up again. It is frustrating.
<?php
$opts = array("guns","knives","ammo");
$selected = array($_POST['selectMenu']);
$revisedOpts = array_diff($opts,$selected);
?>
<form method="post">
<select name='selectMenu'><?php
foreach($revisedOpts as $v) {
echo "<option>".$v."</option>";
}
?></select>
<input onclick="array_diff()" name="Collect" type="submit" value="submit" />
</form>
PHP only acts when the page is loaded, and you load the same code over and over. In order for previously deleted options to stay deleted, you need some kind of data persistence (like a database). Otherwise, you can use javascript to manipulate the select options on the client side browser. Here is a good discussion
If you must bind the action to onclick() and receive the event on the server side, then you will need to use an AJAX call. The onclick calls a separate PHP script which deletes the option and returns some kind of success message.
you want to have a look at some js code to do this. look at something like that http://www.mredkj.com/tutorials/tutorial_mixed2b.html
use jquery
jquery auto suggestion