I have made a simple form and php script which should read a variable from the html form text box and when the user clicks submit a simple message will be displayed saying 'You are searching for songs by artist_name' but when i try i get either a blank page or a message saying undefined. however if i just echo the php variable it displays the value correctly.
i have tried to just use
alert($artist_name) and alert('$artist_name')
But i get Uncaught ReferenceError: $artist_name is not defined. or the alert displays '$artist_name' instead of the value ?
However something like
<?php echo $_GET["artist"]; ?>
successfully get the text ???
and this works ok too.
elseif ($artist_name =="foo") { ?>
<script language="javascript" type="text/javascript">
alert("you enetered foo");
window.location = 'index.html';
</script>
<?php
}
so it is getting the variable ok but i just cant seem to include it in my alert message which is the entire aim of this code, Please can someone show me what i am doing wrong. Many thanks.
my code below:
the html
<form id="form1" action="searchresults.php" method="GET">
<div id="artform">
<fieldset>
<legend> Search for music </legend>
<p> <label> Artist (*): </label> <input type="text" name="artist" id="artist" placeholder="enter an artists" class="add1"></p>
</fieldset>
<input type="submit" class="button1">
<input type="reset" class="button2">
</div>
</form>
the php
<?php
$artist_name = $_GET['artist'];
if (empty($artist_name)) { ?>
<script language="javascript" type="text/javascript">
alert('Field blank !, please try again.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('You are searching for songs by' $artist_name); // the issue is here
window.location = 'index.html';
</script>
<?php
}
?>
You forgot to enclose $artist_name with PHP start and end tag.
alert('You are searching for songs by' $artist_name); // the issue is here
Should've been:
alert('You are searching for songs by <?php echo $artist_name; ?>'); // the issue is here
thank you i got it working, Kek i tried your method but i got an error uncaught SyntaxError: missing ) after argument list, But 125248 comment worked perfectly, i had to include the tags for it to work. Thank you for your time.
Related
I'm in a bit of a problem and cannot seem to spot my mistake.
Here's why:
I want to use a simple PHP if-statement to check if my form has been submitted and if it has, open a JavaScript confirm dialogue to eventually execute deletion from the database. But the code always circles through the wrong statement and it says: "Deleting has been cancelled." (code below).
Thanks for your time.
PHP code:
if (!isset($_POST['delButton'])){
echo "Selection is followed by a confirming window.";
}
else if(isset($_POST['delButton']) && isset($_POST['s_name'])){
if(jsconfirm()){
$sql = MySQL_query("DELETE FROM table_1 WHERE name = \"".$_POST['s_name']."\"");
if($sql){
jsalert("Entry has been deleted!");
}else{
jsalert("Deleting went wrong.");
}
}else{
jsalert("Deleting has been cancelled.");
}
}
Javascript:
function jsalert($s) {
echo "<script>";
echo "alert(\"".$s."\");";
echo "</script>";
}
function jsconfirm(){
echo "<script>";
echo "if(confirm(\"Delete Account?\")){return true;}else{return false;}";
echo "</script>";
}
It's not that your if statement isn't working. It's that you aren't actually communicating between JS and PHP.
When you load a page using a PHP script, then what you echo out becomes the contents of the page.
For example, this PHP would produce this HTML/JS:
<?php
echo "<script>";
echo "alert('hello');"
echo "</script>";
?>
-
<script>
alert('hello');
</script>
If you serve up this file, you will see a page that shows you an alert box stating 'hello'. But that JS code doesn't execute until your PHP code is finished running. So your JS does not communicate with your PHP at all.
If you want something to happen on your server as the result of something done with JS, you're going to need to use AJAX. There are numerous tutorials around the Internet that cover this topic, as it tends to be a bit broad for a Stack Overflow answer.
Perhaps this will help a bit. I'm a beginner myself but this is how I would solve this:
You haven't provided your HTML form, but I have a simple example. This could be a list of the names of your friends and a delete button to wipe them from your friends list. The basic HTML would look something like this:
<!DOCTYPE html>
<html>
<body>
<form>
<p>Peter</p><button>Delete</button>
<p>Frank</p><button>Delete</button>
<p>John</p><button>Delete</button>
</form>
You ultimately want this form to submit data to your PHP function. So you need to tell the form where to send the information upon submit. That's done within the opening tag of the form, like on line 4.
<!DOCTYPE html>
<html>
<body>
<form action="test.php" method="post" id="myForm">//meaning I'm sending the data in the form by *POST* to the file *test.php* in the same folder. The file test.php contains the php code that will update the database.
<p>Peter</p><button >Delete</button>
<p>Frank</p><button >Delete</button>
<p>John</p><button >Delete</button>
</form>
Then you have to have a button to actually trigger the submit action. You can add that function to each Delete button, as below.
<!DOCTYPE html>
<html>
<body>
<form action="test.php" method="post" id="myForm">
<p>Peter</p><button type="submit" >Delete</button>
<p>Frank</p><button type="submit" >Delete</button>
<p>John</p><button type="submit" >Delete</button>
</form>
So, if you would click on Delete now, the data would be posted to your php file. But you don't want that to happen yet! You want to serve a pop-up to your user first and get confirmation. So you make the button trigger a JS function, like this:
<!DOCTYPE html>
<html>
<body>
<form action="test.php" method="post" id="myForm">
<p>Peter</p><button type="submit" onclick="jsalerts()">Delete</button>
<p>Frank</p><button type="submit" onclick="jsalerts()">Delete</button>
<p>John</p><button type="submit" onclick="jsalerts()">Delete</button>
</form>
So, as soon as the Delete button is clicked, the function jsalerts() is executed. This should of course create the confirm box. The basic function could work like this (modified from the JS tutorial at www.w3schools.com):
function jsalerts() {
if (confirm("Press a button!") == true) {
alert("Deleting has been confirmed.");
} else {
alert("Deleting has been cancelled.");
}
}
Trouble is, this function does not stop the Submit action. So regardless of the user's choice, the form will be submitted. And if the user clicked Cancel, that's not what you want. On the other hand, if the user clicked Confirm, you don't want to serve him an alert, you just want the form to continue submitting.
So essentially you want to prevent the default behaviour "submit" of your Delete button if the user clicks Cancel. And JS has a method just for that, unsurprisingly called the preventDefault method. You could implement it like this:
function jsalerts() {
if (confirm("Press a button!") == false) {
alert("Deleting has been cancelled.");
event.preventDefault();
}
So this way the normal process of submitting the form would commence, except when the user clicks cancel.
This means that in your PHP you would only have to create the update database logic and not bother with the cancel or confirm.
So, to wrap it up, this would be your HTML:
<!DOCTYPE html>
<html>
<body>
<form action="test.php" method="post" id="myForm">
<p>Peter</p><button type="submit" onclick="jsalerts()">Delete</button>
<p>Frank</p><button type="submit" onclick="jsalerts()">Delete</button>
<p>John</p><button type="submit" onclick="jsalerts()">Delete</button>
</form>
and this would be your JavaScript:
function jsalerts() {
if (confirm("Press a button!") == false) {
alert("Deleting has been cancelled.");
event.preventDefault();
}
Let me know if it works. And if any more experienced coders have input that can improve my work, please comment. Thanks.
i have tried below code to execute validation function and the data insertion operation the followed code runs correctly on chrome but firefox not showing the message of succesfully insertion..
<input type="submit" onclick="chk()";/>//chk() contains validations
<?
php
if(isset($_POST["submit"]))
{
data insertion code
if(true)
{
<script language="javascript">alert " Adverties added";window.location="adverties.php";</script>
}
}
?>
Put JS in echo:
echo '.. script ... JS code';
I don't know if it's stackoverflow mistake, but " Adv.. " text is not in ( )
So it should be:
if(true)
{
echo '<script language="javascript">alert(" Adverties added");window.location="adverties.php";</script>';
}
I think I got it working.
<form action="[name of the page]" method="post">
<input name="submit" type="submit" onclick="chk()"/>
</form>
<?php
if(isset($_POST["submit"])){
if(true){
echo'<script> alert("Adverties added");window.location = "adverties.php";</script >';
}
}
?>
so I put the <input> inside a <form> that redirects you to the current page and used the POST method.
You also have to use the name property inside of the input tag inorder for $_POST["submit"] to work, I also added the echo to print the JavaScript correctly.
I hope this solves your problems.
p.s. sorry for my bad enlish :)
I've been trying to integrate ckeditor in my php website, and I've encountered the following issue.
Essentially, the content in ckeditor wouldn't appear in the $_POST variable after submitting the form.
I looked the issue up and apparently one has to update the form field with a small piece of code.
So I wrote the corresponding script and linked it to the submit button in order to get the result I want, but $_POST still shows up as empty.
I'm inexperienced with Javascript so the error probably lies there. Any ideas?
cktest.php:
<!DOCTYPE html>
<html>
<head>
<title>A Simple Page with CKEditor</title>
<!-- Make sure the path to CKEditor is correct. -->
<script src="http://localhost/ECLIPSEPHP/ckeditor/ckeditor.js"></script>
</head>
<body>
<form action = <?php echo $_SERVER['PHP_SELF']
?>>
<textarea name="test" id="test" rows="10" cols="80">
This is my textarea to be replaced with CKEditor.
</textarea>
<input type = "submit" name = 'submitButton' id = 'submitButton' value = 'Submit'>
<script>
// Replace the <textarea id="test"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace( 'test' );
</script>
<script type = "text/javascript" src = "http://localhost/ECLIPSEPHP/js/update.js"></script>
</form>
</body>
</html>
<?php
var_dump($_POST);
//echo $_POST['test'];
?>
The javascript supposed to handle the onclick event :
function updateAllMessageForms()
{
for (instance in CKEDITOR.instances) {
CKEDITOR.instances[instance].updateElement();
}
}
var submitButton = document.getElementById('submitButton');
submitButton.onclick = updateAllMessageForms;
There are quite a lot of problems with that code. The first thing to check is to add a method to that form tag: method="post".
See what <form action = <?php echo $_SERVER['PHP_SELF'] ?>> renders. It looks like it could be a wrong. I'm guessing it should be more like <form action="<?php echo $_SERVER['PHP_SELF'] ?>">.
Don't use ' for HTML attribute delimiters, use " instead: 'submitButton' --> "submitButton".
If you edit the updateElement a little: CKEDITOR.instances[instance].updateElement(); alert(1); - do you see the alert? If not, that code is not being called and you need to edit it so that it is.
Don't add spaces between your attribute name, the equals symbol and the value. That looks very strange and could be interpreted wrong or it could send Internet Explorer into quirks mode. Try to change this style: type = "submit" to type="submit" and keep up with that style.
Remember that it's often a good idea to look at the Rendered source in the browser to see what the browser actually gets. Happy coding!
I have following code at s.php:
<?php
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['p'] = $_POST['p'];
}
?>
<form action="s2.php" method"post">
<input type="text" name="p"/>
<input type="submit" name="Submit" value="Submit!" />
</form>
And At s2.php
<?php
session_start();
?>
<?php
echo 'This is especially for ='.$_SESSION['p'];
?>
After entering value in input field and clicking the submit button, it take to next page and change the browser link to some thing like /s2.php?p=inputvalue&Submit=Submit.
I want to show the value at s2.php that was entered in the input field at s.php.
I have placed the echo code, but nothing shows up (I have tested on different servers).
The problem is solved. Thank you.
Solution: at s2.php (action page) we have to use the following code:
echo 'This is especially for ='.$_POST['p'];
Thanks
I get an insure after uploading my PHP project into an internet host(000webhost.com). It works fine at my localhost but doesn't at internet host. Here is my code:
//view.php (use a hidden iframe to received data after submitting)
<form action="model.php" method="POST" target="my_iframe" id="my_form" style="display: none;">
<input type="hidden" name="user" id="user" value=""/>
<input type="hidden" name="user_lastname" id="user_lastname" value=""/>
<input type="hidden" name="user_firstname" id="user_lastname" value=""/>
</form>
<iframe name="my_iframe" id="my_iframe" style="display: none;"></iframe>
<div id="sent">Sent</div>
<srcipt type="text/javascript">
jQuery('#sent').unbind('click').click(funtion(){
jQuery('#user').val("an user name");
jQuery('#user_lastname').val("a last name");
jQuery('#user_firstname').val("a first name");
jQuery('#my_form').submit();
jQuery('#my_iframe').unbind('load').load(function(){
if(jQuery(this).contents().text()!='success')
alert('Update failed');
else
alert('Update successful');
});
});
</script>
//model.php
if(UpdateUser($_REQUEST['user'],$_REQUEST['user_firstname']),$_REQUEST['user_lastname'])==true)
echo 'success';
else
echo 'Fail in updating';
At my localhost, i get an message "Update Successful" but at my internet host, i get an message "Update failed". Thank for advances. Sorry because of my bad English.
---EDIT----
I change style="display:block" of iframe to show its content. I see that iframe's content is "success" but i still get message "Update failed".it means:
I corrected the misspellings (there was another: function spelled "funtion" in the first line of the script).
The jQuery script is working so the problem must be with the PHP page. Maybe 000webhost puts some output before your PHP page's output or maybe that update is failing. Check the output of the PHP page.