JS and PHP value passing issue - javascript

<?php
for ($i=1; $i <10 ; $i++) {
$A='A'.$i;
echo "<button onclick='selectSeat(id)' id='$A'>".$A."</button>";
}?>
<script type="text/javascript">
function selectSeat (id)
{
alert ("Button pressed ".id);
}
</script>
above is my seating map.... i want to check if if seat is reserved or not in DB through php but i don't know how to get it done through java script. i'm using java script to prompt user in if seat is reserved or not.

I don't know if it's just a typo, but you are using the wrong operator to concatenate strings in javastring:
alert ("Button pressed ".id);
Should be
alert ("Button pressed "+id);

One way is to create a hidden input field using PHP with the value you want to send to javascript. Then you can get the value with javascript (either vanilla or jQuery) using $('#hidden-field').val() (for example).

Related

Pass javaScript variable to php or workaround for just php

I'm trying to get a JavaScript variable to php without refreshing the page or submitting the form. I think my problem is that php is on the server side. I have a button that's set in a php echo...
<input type='button' value='Edit' name='editbtn' onmouseup='reply_click(this.id)' onclick='edit()' id = '" . $row['id'] . "'
This button works correctly. I didn't provide all the code surrounding it, just the part in question. I can inspect the button element in the browser and the "id" shows the correct id number.
Next I'm using some test JavaScript in an effort to get the id to php with having to refresh the page or submit the form.
echo "<script type='text/javascript'>
function reply_click(clicked_id){
var selid = clicked_id;
alert(selid);
document.writeln(selid);
} </script>";
This code works. I get the correct id to show in an alert or document.writeln. The problem comes when I try to set this to a php variable like this...
$testid = "<script type='text/javascript'>document.writeln(selid);</script>";
echo $testid;
$testid returns nothing.
Is there a way to do this or is there a workaround using only php? I need to do this without submitting the form or refreshing the page. Is that possible? The end goal is to create another MySQL statement using that id value. For example: SELECT * FROM table WHERE id = the $testid from JavaScript.
Thank you!

jQuery responds with html table containing a (form in each row plus jquery code to edit each row) newly created forms ignore submit()

Three days and I cannot find an answer to this or a solution. I am far from being a jQuery guy.
User arrives at a php page that shows a form to choose the language code and submit. Form gets submitted and the jQuery response builds a table in a div container in the original page. So far so good. The resulting table contains hundreds of rows where the language variables can be edited. On clicking the edit button, I get nothing, no errors in console, nothing. If I use on click, I can fire an alert. On submit, bind, and many others do not work.
I am attempting to load the table, perform basic editing, submit the edits to the db, and refresh the div.table.row with the edited results. I have not gotten to the part where I refresh the rows with the edited data, still stuck trying to submit the dynamic form in each row.
One interesting thing that happens when I use on click, it works with just the first button I click on, it somehow disables all the other rows. I need to be able to submit a row for changes, have that row refresh and move on to the next row all without redirecting. Is this too much to ask? Or should I just move them from page to page?
The ajax php page returns the table using the following code:
if(!empty($_POST['edit_language']) && $_POST['edit_language'] == 1){
edit_language($_POST['lang']); //call function to edit language
} else {
echo "You got here but no variables<br>"; //testing
print_r($_POST); //testing
}
function edit_language($lang){
//query table to get language vars list
$sql = "SELECT lang_site.lid, lang_codes.iso_code, lang_codes.`language`, lang_varnames.varid, lang_varnames.varname, lang_site.varval FROM lang_codes LEFT JOIN lang_site ON lang_site.langid = lang_codes.langid LEFT JOIN lang_varnames ON lang_site.varid = lang_varnames.varid where lang_codes.iso_code = '" . $lang . "'";
$result = db_query($sql);
//generate report table
echo "<table cellspacing='0' border='1'><tr><th>Count</th><th>Language</th><th>Variable Id</th><th>Variable Name</th><th>Variable Value</th><th>Edit</th></tr>";
$a=1; //count the rows to number the rows for easy viewing
while($data = db_fetch_assoc($result)){
//create form in each tr
echo "<form name='edit_" . $data['lid'] . "' id='edit_" . $data['lid'] . "' action=''><tr><td>" . $a++ . "</td><td>" . $data['language'] . "</td><td>" . $data['varid'] . "</td><td>" . $data['varname'] . "</td><td><textarea class='form-control' name='varval' id='varval' cols='100' wrap='virtual'>" . $data['varval'] . "</textarea></td><td id='editresponse'><button type='submit' class='button' type='submit' id='but_" . $data['lid'] . "'>Edit</button></td></tr></form>";
?>
//jquery to post edits
<script language="javascript">
$(document).ready(function()
{ //using $.ajax() function
//alert("document ready"); //this alerts each row
//this function will not work, no errors, simply nothing
$(document).on("submit", "#edit_<?php echo $data["lid"]; ?>", function(e)
{
//alert("button clicked"); //this does not work
var data = $("#edit_<?php echo $data["lid"]; ?>").serialize();
$.ajax({
type : "POST",
url : "/lang/ajax_langs.php",
data : data,
success : function(response)
{
{
$("#editresponse").html(response);
};
}
});
e.preventDefault();
});
});
</script>
<?php
}
echo "</table>";
}
?>
How do I get the jQuery to submit the individual form in each row to effect an edit and then refresh each row individually as languages are edited? I need to add another form to each row to delete an entry then remove the row dynamically, but I am stuck here without being able to submit any of these forms.
I should add that this generated form shows well in the website, but is not shown in view source. Using chrome browser. I'm open to any suggestions. Thanks in advance for reading. I seriously hope I can find a solution here. Otherwise, I'm back to the 90's on this software.
It's not working because you are only submitting one id. You need to make each of your forms be of the same class. Therefore, any form clicked will be processed. Like
<form class="myForm" .....
Then, the Jquery would look like this
$(".myForm").submit(function(e){
var inputToChange = this.varval; // saw input varVal, but use whatev
var dataToChange = inputToChange.value;
// then send the data to php for processing using ajax or post
Then, use inputToChange.value = whatever new value you want to put into the form input or other div/td etc. you want to change. If you want to change data that isn't in an input, just make that td a text input and disable it from being edited by the client
<input type="text" name="lname" disabled>
then on callback send new data to this.lname.value = (many other ways to do this as well)
Don't forget, if you send through ajax you want to stop the form itself from submitting so use e.preventDefault()

how to get confirmation box value in php

condition :
when i submit a button, the system will check database A and run query data using mysql. if any data exist, a popup box will ask if i want to proceed with the action, if i answer ok, the system will check database B and run query and ask the same question. if i answer ok, the system will update the database. can i use javascript in php for this situation?
my current script
if($_POST['submit']){
$check = $db->check_table($idnumber);
if($check>1){ echo "<script type='text/javascript'>confirm('Proceed?')</script>"; }
}
how do i get the value of the confirmation box when i click ok or cancel so that i can proceed to check the second table.
I would just enclose your HTML in an if statement in PHP
<?
if($_POST['submit']){
$check = $db->check_table($idnumber);
if($check>1){?>
<script type='text/javascript'>$(function() {confirm('Proceed?')});</script>
<?
}
}
?>

Blank alert confirmation box from javascript function, when called from HTML form

I'm currently programming a website system as part of my studies. I'm using PHP,HTML and Javascript throughout the project. I'm a novice programmer and i'm learning as i go along.
My current problem is that when a form submit button is pressed e.g. UPDATE or DELETE. I wanted a confirmation box asking the user 'Are you sure you want to submit?' using javascript. However whenever I click the button , an empty alert box appears even though there is text inside the JS function. Please look at the code below ! :
function confirm()
{
return alert("Are you want to submit the form"?);
}
</script>
The html code is echoed in a php while loop
echo "<td>" . "<input type=submit name=update value=update onclick='confirm();'" . "> </td>";
echo "<td>" . "<input type=submit name=delete value=delete onclick='confirm();'" . "> </td>";
I am grateful for anyone taking their time to help !
Problem solved guys! appreciate all your help, was an error in with the ? in the javascript function. Funnily enough, it wasn't using confirm as my function name! won't use confirm next time for good practice.
return alert("Are you want to submit the form"?); <- ? outside of ""
Corrected
return alert("Are you want to submit the form?");
if(confirm("question")){
// yes
} else {
//no
}
confirm() is a javascript reserved word
confirm() is already a Javascript function. The question mark should also remain inside the double-quotes. Change your function name for clarity:
function confirmForm()
{
return alert("Are you want to submit the form?");
}
echo "<td>" . "<input type=submit name=update value=update onclick='confirmForm();'" . "> </td>";

How to check if input type button is pressed in PHP?

The isset() function can be used to check if the input type submit is pressed, but is there a way to check if the input type button is pressed?
In my code the button does nothing but call a function on the .Onclick() event which then refreshes the page and makes a database entry inside PHP...and I want it to make the entry only after the button is pressed...and I can't use the submit type for other reasons...following is some code:
function send()
{
var events = document.getElementById("event").value;
location.href = "calm.php?day=" + xx + "&month=" + yy + "&year=" +
zz + "&events=" + events;
}
<input name="Button"
type="button"
id="submit"
onclick="send(this.form)"
value="Button" />
<?php
session_start();
include_once "connect_to_mysql.php";
$day = $_GET['day'];
$month = $_GET['month'];
$year = $_GET['year'];
$events = $_GET['events'];
$userid = $_SESSION['id'];
if (isset($_POST['button']))
{
$sql = mysql_query("INSERT INTO events (userid,month,day,year,events)
VALUES('$userid','$month','$day', '$year','$events')")
or die (mysql_error());
}
?>
isset($_POST['Button']) you just missed the capital B. You need to match it the same as the input's name attribute.
you need to do that using ajax. so when a user clicks this button the page won't refresh.. there should be a page behind the seen does that for the user. whenever he clicks the button it accesses that page and upload whatever data you want...
edit:
or you can just add a hidden input in the form when the button is clicked the hidden input value changes to i.e. true... then from php codes you can use the isset or other functions to validate whether the user clicked the button or not...
In the example you have, the simplest approach would be to add an extra variable to the parameters passed in &button_press=true or something like that and then you would know that the button had been pressed when you are receiving the information
'locrizak' answered right . Your button name is 'Button' and you tried to check presence of click on 'button' both are different .
In such case if you are unsure of what is wrong , you may print the entire POST array using
print_r($_POST)
This will display all submitted values from form including button
Using isset() is the correct method to check whether a form element is present or not
Use
if(isset($_POST['Button'])){
//code block for insertion,validation etc //
}
isset() function does not works with input type=button. so either we have to use input type=submit instead of button or some hidden type if we still want to use button.

Categories

Resources